简体   繁体   中英

How to find all items in a sorted List<T> using .NET 2.0?

How can I find all items in a sorted collection?

Basically I have a List<T> collection (large one). First I want to sort it so that the subsequent calls for finding all matches could be performed as fast as possible. i would like to use some build-in mechanisms rather than performing manual searches.

I initially used FindAll but that probably will enumerate the entire collection and I need to speed it up.

It has to be done under .NET 2.0

EDIT

  1. I have a collection: List<Custom>

Custom is a class with 3 public fields (it is a container)

  1. Then I sort it:

    collection.Sort((x, y) => x.Id.CompareTo(y.Id)); // Id is a Guid

  2. Now I need to find all items in a collection by Id without enumerating all items in a collection.

IEqualityComparer<T> and IEquatable<T> interfaces have been available since .NET 2.0 and this framework version introduced generic dictionaries : Dictionary<TKey, TValue> .

What you can do is indexing . For example, if you want to get your objects by id in a constant time, in addition to adding them to your list, you can add them to a Dictionary<Guid, Custom> .

Now you can get objects by id in a constant time:

Custom custom = all[someGuid];

This approach can be followed by the rest of your class properties.

Why I've talked about IEqualityComparer<T> and IEquatable<T> ? Because if one of the whole properties is another custom class, maybe you'll need to implement one of the so-called interfaces (and/or override Object.Equals and Object.GetHashCode ) to implement a custom equality, so dictionary keys will be unique.

If you want to combine more than a property, maybe you'll need to define a class to provide one or more properties:

public class Args
{
    public CustomA CustomA;
    public CustomB CustomB; 
}

...and you can implement an IEqualityComparer<T> for Args to provide a combined hash code and be able to use Args as dictionary key. This would be like looking for an object which matches all provided properties in a given Args instance... (ie like using an && operator in a where clause...).

Did you try Sort from the Collections namespace? Try providing a Comparer.

List.Sort Method () .NET Framework 2.0

Why not use a 2D array instead of sorted list, where the first dimension is the GUIDs?

OR

Sort the list, use IndexOf to get the first index of your GUID. Then, use RemoveRange to remove all items before the index you got. And lastly, use Find on the remaining range to get the first GUID that doesn't match your GUID.

See here: LINQ support on .NET 2.0

The easiest way I can think of is to use Linq:

wrong way

 List<Order> objListOrder = new List<Order>();
    GetOrderList(objListOrder); // fill list of orders

Right way

List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();

Use LINQ with .NET Framework 2.0

LINQBridge (from the author of the excellent LINQPad) is a small 60KB assembly that when combined with the multi-targeting capabilities of Visual Studio 2008 gives the ability to write code which does LINQ to Objects (not XML or SQL) but runs fine on a machine with just the .NET Framework 2.0 installed. Very clever

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM