BinarySearch array of objects by ID

拜拜、爱过 提交于 2020-01-23 07:05:06

问题


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?


回答1:


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:


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>());



回答3:


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

return x.ID > y.ID;


来源:https://stackoverflow.com/questions/26786135/binarysearch-array-of-objects-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!