vote up 1 vote down star

I'm looking to sort an array of about 200-300 objects, sorting on a specific key and a given order (asc/desc). The order of results must be consistent and stable.

What would be the best algorithm to use, and could you provide an example of it's implementation in javascript?

Thanks!

flag
2  
Since at least Chrome's array sort doesn't seem to be stable, relying on the built-in array sort is not an option for you. – Nosredna Sep 15 at 14:56
To summarize: I went with a hand rolled merge sort due to Array.sort stability inconsistencies between modern browsers (mainly chrome not implementing a stable sort at the time of this comment). Thanks everyone for your help! – Bill Casarin Sep 15 at 15:30

5 Answers

vote up 6 vote down check

Since you are looking for something stable, the merge sort should do.

http://en.literateprograms.org/Merge_sort_(JavaScript)

EDIT:

According to this post, it looks like Array.Sort in some implementations uses a merge sort.

link|flag
++ Merge sort is my favorite. It's simple and stable with no bad worst cases. – Mike Dunlavey Sep 15 at 15:05
I'll look into this. Thanks! – Bill Casarin Sep 15 at 15:09
vote up 2 vote down

It is possible to get a stable sorting from a non-stable sort function.

Before sorting you get the position of all the elements. In your sort condition, if both elements are equal, then you sort by the position.

Tada! You've got a stable sort.

I've written an article about it on my blog if you want to know more about this technique and how to implement it: http://blog.vjeux.com/2010/javascript/javascript-sorting-table.html

link|flag
vote up 1 vote down

Will the built in sort function do? http://www.w3schools.com/jsref/jsref%5Fsort.asp

link|flag
1  
I don't think the built-in sort uses a stable algorithm – Philippe Leybaert Sep 15 at 14:45
I was thinking the same thing. – Kevin Sep 15 at 14:46
Oh, bummer. I second the suggested merge sort then :) – finalman Sep 15 at 14:48
1  
Good point. It's not guaranteed, but browsers have moved toward using stable implementations. IE 6+ is stable. Firefox 3 is. Safari is. Not sure about Chrome (I'd hope it is). Or Opera (it used to be unstable--I hope it's fixed now). – Nosredna Sep 15 at 14:50
Chrome not stable. Does anyone know if Opera 10 is? – Nosredna Sep 15 at 14:57
vote up 1 vote down

Check this article:

http://www.hedgerwow.com/360/dhtml/js%5Farray%5Fstable%5Fsort.html

It seems that only FF 2.0 and Opera 9 and earlier have a non-stable sort implementation. All other mainstream browsers do.

link|flag
1  
Yikes. I just tried Chrome and it doesn't look stable, so I'd say assuming stable in modern browsers is a big mistake. – Nosredna Sep 15 at 14:54
That's why I was cautious and said "it seems" :-) Anyhow, I wouldn't rely on it. – Philippe Leybaert Sep 15 at 14:58
vote up 0 vote down

You should be able to utilize the core Array.sort method. Example...

var data = []; //populate array

function sortByName(a, b) {
  var x = a.LastName.toLowerCase();
  var y = b.LastName.toLowerCase();
  return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

data.sort(sortByName);

Note: this only sorts ascending. But you can take care of that by tweaking the function or using Array.reverse.

link|flag
Note that the built-in array sort is not stable in (at least) Chrome, so it violates one of your requirements. – Nosredna Sep 15 at 15:00
Please elaborate on the "not stable". I would have to say it is feasible to rely on the core Javascript framework. – Josh Stodola Sep 15 at 15:01
1  
The question specifically asked for a stable sort, but the built-in array sort is not required to be stable (according to the ECMA standard). – Philippe Leybaert Sep 15 at 15:01
2  
ECMAScript Language Specification 3rd Edition section 15.4.4.11: "The elements of this array are sorted. The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order)." So Google's implementation is to spec, not a bug. developer.mozilla.org/en/… also notes that although Mozilla's current implementation is stable, "the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this." – NickFitz Sep 15 at 15:48
1  
Agree that Google's implementation is not a bug. However, it's in their best interest to match what the other browsers are doing, as they did with returning objects in the order they were added instead of an arbitrary order (as the spec allows). @Josh, if you've used this method for years, your results were different for users of FF2 than they were for IE users. I'm guessing that you didn't need a stable sort. Whether you need one depends on the situation. – Nosredna Sep 15 at 16:11
show 11 more comments

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.