Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  83] [ 4]  / answers: 1 / hits: 63657  / 15 Years ago, tue, september 15, 2009, 12:00:00

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!


More From » algorithm

 Answers
43

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


[#98695] Wednesday, September 9, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
theron

Total Points: 168
Total Questions: 93
Total Answers: 94

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
;