2

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?

King Chan
  • 4,212
  • 10
  • 46
  • 78

3 Answers3

2

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

Use linq.orderby passing in your IComparable.

Community
  • 1
  • 1
FiveTools
  • 5,970
  • 15
  • 62
  • 84
  • I don't really understand how can I apply that into databinding with DataGrid to do the sorting... I am not sorting it with Linq programmatically... – King Chan Apr 19 '12 at 12:52
  • Oh wait, I missed your last sentense. I think you are right. Thanks. – King Chan Apr 19 '12 at 18:14
1

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

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
0

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.

David Yaw
  • 27,383
  • 4
  • 60
  • 93