Binary Search a List of custom data types to match just one field

前端 未结 4 1689
清酒与你
清酒与你 2021-01-21 15:07

I have a List:

List allStudents = new List(); 

that contains over 94,000 Student objects, where Student is define

4条回答
  •  情书的邮戳
    2021-01-21 15:13

    List.BinarySearch is a good solution and works like you would expect. Here's a link that shows a solution similar to what you'll need for the IComparer. Their example doesn't use the Generic IComparer, though.

    public class CompareCustomDataType : IComparer {
    
      public int Compare(Student x, Student y)
      {
        if (x == y)    return 0;
        if (x == null) return -1;
        if (y == null) return 1;
    
        return String.Compare(x.Surname, y.Surname);
      }
    ...
    }
    

提交回复
热议问题