5

Good day!

I have a List of ValueObj:

class ValueObj
{
   int ID;
   float value;
}

How to get binary search objects by id? (List tempValues)

I make ValueComparer class,but dont know am i right?

class ValueComparer<ValueObj>
{
   public int Compare(ValueObjx, ValueObjy)
   {
       if (x == y) return 0;
       if (x == null) return -1;
       if (y == null) return 1;

       return -1; ///???
   }
}

I need to sort List by ID. Like that?:

tempValues.Sort(new ValueComparer());

And how to use BinarySearch?

3 Answers 3

3

first of all you should make your class like this. your fields were not public and you can not access them, also public fields are not good so you should change them to property

    class ValueObj
    {      
        public int ID { get; set; }
        public float value { get; set; };
    }

and your comparer like this

class ValueComparer : IComparable<ValueObj>
{
  public int Compare(ValueObj x, ValueObj y)
  {
      if (ReferenceEquals(x, y)) return 0;
      if (x == null) return -1;
      if (y == null) return 1;

      return x.ID == y.ID ? 0 :
               x.ID > y.ID ? 1 : -1;
  }
}

then you have a list like

var tempValues = new List<ValueObj>();
//many items are added here

you should always sort your list before performing a binary serach

 //this does not modify the tempValues and generate a new sorted list
 var sortedList = tempValues.OrderBy(x => x.ID).ToList();

or you can sort tempValues directly

//tempValues is modified in this method and order of items get changed
tempValues.Sort(new ValueComparer<ValueObj>());

now you want to find index of specific ValueObj

var index = sortedList.BinarySearch(specificValueObj, new ValueComparer<ValueObj>());

or if you have used second method of sorting

var index = tempValues.BinarySearch(specificValueObj, new ValueComparer<ValueObj>());
Sign up to request clarification or add additional context in comments.

Comments

0

The List class in C# has a BinarySearch method you can use with your Comparable.

Your type:

class ValueObj
{
    public int ID{ get; set;}
    public float value { get; set;}
}

Your comparison class (do not forget to implement the correct interface!):

class ValueObjIDComparer : IComparable<ValueObj>
 {

    public int Compare(ValueObj x, ValueObj y)
    {
        if (x == null) return -1;
        if (y == null) return 1;

        if (x.ID == y.ID) return 0;            

        return x.ID > y.ID ? 1 : -1;
    }
 }

Executing a binary search:

List<ValueObj> myList = new List<ValueObj>();
myList.Add(new ValueObj(){ID=1});
myList.Add(new ValueObj(){ID=2});
// ...

int idToFind = 2;
myList.Sort(new ValueObjIDComparer());
int indexOfItem = myList.BinarySearch(idToFind, new ValueObjIDComparer()); 

There are many more operations you can perform on lists. See the documentation here.

2 Comments

@HamidP You're right, thanks for pointing that out. For some reason I thought the method would do that for you. Answer has been updated.
You're searching on idToFind. Wich is an int. But the BinarySearch method needs an ValueObj as first parameter.
0

If you want to sort by ID, you could simply compare the IDs in Compare method:

return x.ID > y.ID;

1 Comment

That return statement returns a bool not an int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.