Why is my List.Sort method in C# reversing the order of my list?

Posted by Fiona Holder on Stack Overflow See other posts from Stack Overflow or by Fiona Holder
Published on 2010-04-20T10:26:35Z Indexed on 2010/04/20 10:33 UTC
Read the original article Hit count: 241

Filed under:
|
|
|

I have a list of items in a generic list:

  • A1 (sort index 1)
  • A2 (sort index 2)
  • B1 (sort index 3)
  • B2 (sort index 3)
  • B3 (sort index 3)

The comparator on them takes the form:

this.sortIndex.CompareTo(other.sortIndex)

When I do a List.Sort() on the list of items, I get the following order out:

  • A1
  • A2
  • B3
  • B2
  • B1

It has obviously worked in the sense that the sort indexes are in the right order, but I really don't want it to be re-ordering the 'B' items.

Is there any tweak I can make to my comparator to fix this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET