In the book "Javascript: The Good Parts", the author mentioned the concept "stable" in page 81. Link to Google book
However, I find that the example given by the book is irrelevant to whether the sort is stable or not. Wiki
Am I missing anything here?
So the example in the book is as follow:
var s = [ {first: 'Joe', last: 'Besser'}, {first: 'Moe', last: 'Howard'}, {first: 'Joe', last: 'DeRita'}, {first: 'Shemp', last: 'Howard'}, {first: 'Larry', last: 'Fine'}, {first: 'Curly', last: 'Howard'} ];
The sort method is not stable, so:
s.sort(by('first')).sort(by('last'));
is not guaranteed to produce the correct sequence.
But this example actually doesn't prove if the sorting is stable or not. If sort by first then sort by last, the sort by first part will be overridden. The result is below:
[ { first: 'Joe', last: 'Besser' },
{ first: 'Joe', last: 'DeRita' },
{ first: 'Larry', last: 'Fine' },
{ first: 'Curly', last: 'Howard' },
{ first: 'Moe', last: 'Howard' },
{ first: 'Shemp', last: 'Howard' } ]
I know JS sort is not guaranteed stable. Here and here. But I don't think the book has treated the topic in the right way. My question is that I don't know if my understanding is correct or not. If I'm wrong I want to know why.