0

I understand there's a difference between the sorting algorithms in List<T>.Sort and Enumerable.OrderBy. The latter is stable, meaning where elements are equal, it preserves their original order.

It's all very well repeating the definition, but to teach this, I'd like to demonstrate a simple example where the algorithms differ in results.

I found came up with this example "Given the seven Weasley siblings from oldest to youngest, sort them by name length".

var weasleys = new List<string>{"Bill", "Charlie", "Percy", "Fred", "George", "Ron", "Ginny"};

In this case, OrderBy

weasleys.OrderBy(x => x.Length)

gives [Ron, Bill, Fred, Percy, Ginny, George, Charlie]. Note that 'Bill' and 'Fred' are the same length but Bill is older so Bill comes first.

Whereas List.Sort

weasleys.Sort((x, y) => x.Length.CompareTo(y.Length));

gives [Ron, Fred, Bill, Ginny, Percy, George, Charlie].

My example had seven items. Is there a simpler case with fewer items? What's the smallest list on which the algorithms give different results?

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • 1
    take a look at this answer, http://stackoverflow.com/questions/1832684/c-sharp-sort-and-orderby-comparison – ZaoTaoBao Nov 26 '13 at 12:13
  • Your example has one line of setup, shared between both cases, and one line for each case. How much less could you possibly hope for? – Will Dean Nov 26 '13 at 12:14
  • In case others guessed wrong, please elaborate what exactly do you not understand in this example, and how you'd like it to have "fewer items".. I for example fail to guess it. – quetzalcoatl Nov 26 '13 at 12:15
  • @ZaoTaoBao He's not interested in the speed of the methods, he's interested if the difference can be shown in a simpler way. – Tobberoth Nov 26 '13 at 12:16
  • 1
    sorry, i'm really old... Sort sorts the present list while OrderBy returns a new list, and sort() performs and unstable sort, while orderBy performs a stable sort, if the key of 2 elements are equal, the order of element is preserved! – ZaoTaoBao Nov 26 '13 at 12:19
  • Guys, please. The author asks `what is the smallest example` and `is there a simpler example`. Unless he changes the questions, this a code compression quiz, not question about facts and algorithms. – quetzalcoatl Nov 26 '13 at 12:22
  • 1
    Actually it returns same sequence on .net 4.5: http://www.dotnetfiddle.net/yTQvBl – Andrey Nov 26 '13 at 12:40

2 Answers2

3

This property of sorting algorithm is called stability. List<T>.Sort is explicitly unstable:

This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

http://msdn.microsoft.com/en-us/library/b0zbh7b6(v=vs.110).aspx

Where OrderBy is explicitly stable:

This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.

http://msdn.microsoft.com/en-us/library/bb534966(v=vs.110).aspx

Andrey
  • 59,039
  • 12
  • 119
  • 163
  • What's the smallest list on which the algorithms give different results? – Colonel Panic Nov 26 '13 at 13:42
  • @ColonelPanic yours list doesn't work on .net 4.5 (see my comment on the question) so I can't reproduce it at all. I don't really understand the point of your question, why do you care about this "smallest list". – Andrey Nov 26 '13 at 13:53
0

I'm not sure, but maybe: Sort is just a method. OrderBy is extension method (Linq) http://msdn.microsoft.com/en-us/library/vstudio/bb383982.aspx OrderBy was made later, and works with IEnumerable. And it's a part of linq, which gives you alot more features when you want to combine orderby with other linq actions.

apocalypse
  • 5,764
  • 9
  • 47
  • 95
  • Whether it is extension method or build in method is completely inrealed. The only thing that matters is underlying algorithm. – Andrey Nov 26 '13 at 12:19
  • I thought he asks why there is 2 methods doing "same" job. So I think 1st method is designed by person1 and second method is designed by person2 for new system. U can ofcourse downvote, im not an expert. – apocalypse Nov 26 '13 at 12:25
  • I don't downvote competing answers. But the thing is that we can only guess what the reason was, it can only be known if Eric Lippert jumps in, but it is very unlikely. So from practical standpoint what is important is that their behavior is well documented. – Andrey Nov 26 '13 at 12:29