Can I search for a sorted <T> list faster when it is sorted in the fields I'm looking for?

I would like the sorting List<T>in the fields I was looking to speed up the search. Suppose I have List<Person>10,000 and List<Car>10,000 in the object model. I will loop the list of Persons in the Model and want to find a Car with the property c.Owner == person.Name.

public static Car Car(Model model, Person person)
        {
            return model.Cars.Find(
                 delegate(Car c)
                 {
                     return c.Owner.Equals(person.Name);
                 });
        }

Sorting a list of cars from the owner of the property does not speed up the cycle?

I thought maybe I should use it BinarySearch, but overloads BinarySearchdon't allow delegates. What should I use BinarySearchwhen you need to give the car you want to find as a parameter?

+3
source share
1 answer

List<T>.BinarySearch , , IComparer<T>. (CarByOwnerComparer : IComparer<Car>), , . , , , . (, ) , , , ProjectionComparer .

, , . , SortedList<,> SortedDictionary<,> O(logn). , Dictionary<,>.

+5

Source: https://habr.com/ru/post/1790414/


All Articles