Question

As titled.

We all know if we want a class to be comparable and use in sorting i.e DataGrid, we will implement IComparable.

But for IEnumerable how can I do that?

I have a collection of a IEnumerable, we want to compare each IEnumerable to each other and do a sorting.

So say a collection List<IEnumerable<char>> that contains:

IEnumerable<char> EnumableA contains: "d", "e", "f"

IEnumerable<char> EnumableB contains: "d", "e", "c"

If we bind the collection List<IEnumerable<char>> to a DataGrid, when we sort them in acs order, the order will be EnumableB 1st, then EnumableA the 2nd.

I do think of solution such as store the EnumableA into an object whichs implment IComparable, but then this would require to create another collection of objects, which will be expensive.

So is it possible or anyway to APPEND a IComparable interface and my sorting implmentation to the IEnuerable<char> so it will be sortable?

Était-ce utile?

La solution

Check out this post: Use own IComparer<T> with Linq OrderBy

Use linq.orderby passing in your IComparable.

Autres conseils

Many of the places where IComparable can be used will also accept IComparer. It may be easier to write an IComparer<IEnumerable<char>.

The way to do what you want to do would be to write a new class that implements both IEnumerable<char> and IComparable.

I'd probably implement IList<char>, and have a regular List<char> as the backing list, implementing all of IList with the same calls to the backing list, and implement IComparable however you need.

Of course, you could go the other way, and use the string class. It implements both IComparable and IEnumerable<char>, so that might be appropriate for what you want.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top