Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I found a great method to sort an array of objects based on one of the properties as defined at:

Sorting objects in an array by a field value in JavaScript

Using that function works perfectly for a single sort (on all browsers), and even a sort within another sort EXCEPT using Google Chrome! Here is Ege Özcan's great sort routine for arrays of objects

function dynamicSort(property) { 
    return function (a,b) {
        return (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
    }
}

Using an array named "Data" (of course, my array has many more object pairs)...

var Data = [{Category: "Business", Value: "ABC"},{Category:"Personal", Value:"XYZ"}];

I can get a proper sort where the order is listed as all the values within each category by doing this...

Data.sort(dynamicSort("Value"));
Data.sort(dynamicSort("Category"));

By first sorting on Value, and then by Category, my array puts all values in sorted order with all the Business-base values listed first and then all the Personal-based values. Perfect! Except in Chrome where the data is sorted properly by category, but the order of the values within each category seems rather random.

Does any one know of a better way to do a sort within a sort that would also work in Chrome?

share|improve this question
2  
sorting by A and then independently sorting by B is not the same as sorting by A and B. If it worked on some browsers it was a fluke. – Alnitak Jul 8 '12 at 21:54

1 Answer

I created a multi-parameter version of that dynamicSort function:

function dynamicSort(property) { 
    return function (obj1,obj2) {
        return obj1[property] > obj2[property] ? 1
            : obj1[property] < obj2[property] ? -1 : 0;
    }
}

function dynamicSortMultiple() {
    /*
     * save the arguments object as it will be overwritten
     * note that arguments object is an array-like object
     * consisting of the names of the properties to sort by
     */
    var props = arguments;
    return function (obj1, obj2) {
        var i = 0, result = 0, numberOfProperties = props.length;
        /* try getting a different result from 0 (equal)
         * as long as we have extra properties to compare
         */
        while(result === 0 && i < numberOfProperties) {
            result = dynamicSort(props[i])(obj1, obj2);
            i++;
        }
        return result;
    }
}

I created an array as follows:

var arr = [
    {a:"a",b:"a",c:"a"},
    {a:"b",b:"a",c:"b"},
    {a:"b",b:"a",c:"a"},
    {a:"b",b:"a",c:"b"},
    {a:"b",b:"b",c:"a"},
    {a:"b",b:"b",c:"b"},
    {a:"b",b:"b",c:"a"},
    {a:"b",b:"b",c:"b"},
    {a:"b",b:"b",c:"a"},
    {a:"b",b:"b",c:"b"},
    {a:"b",b:"b",c:"a"},
    {a:"c",b:"b",c:"b"},
    {a:"c",b:"c",c:"a"}
];

and it worked when I did,

arr.sort(dynamicSortMultiple("c","b","a"));

And here is a working example: http://jsfiddle.net/ZXedp/

share|improve this answer
Perfect. Works like a charm in all browsers. Thanks Ege!! – Colin Houghton Jul 8 '12 at 2:47
You are welcome. Would you mind accepting the answer? =) – Ege Özcan Jul 8 '12 at 9:34
1  
And don't forget to upvote him! :) – tftd Jul 8 '12 at 21:50
Thanks! But, is this capable of sorting mixed ascending and descending order? – jzap May 9 at 3:12
this one does, also with multiple properties: stackoverflow.com/questions/1129216/… – Ege Özcan May 10 at 8:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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