Cornwell
Cornwell

Reputation: 3410

Sorting an object

I am trying to order an object that I "converted" from JSON. I'm trying to use this function that I found on this question, but it does not seem to work in my case:

function sortJsonArrayByProperty(objArray, prop, direction){
    if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments");
    var direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending

    if (objArray && objArray.constructor===Array){
        var propPath = (prop.constructor===Array) ? prop : prop.split(".");
        objArray.sort(function(a,b){
            for (var p in propPath){
                if (a[propPath[p]] && b[propPath[p]]){
                    a = a[propPath[p]];
                    b = b[propPath[p]];
                }
            }
            // convert numeric strings to integers
            a = a.match(/^\d+$/) ? +a : a;
            b = b.match(/^\d+$/) ? +b : b;
            return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) );
        });
    }
}

And here is my JsFiddle

I want to sort it in 3 different ways:

If this object wasn't so nested I would know how to do it. Could I adapt that function?

Upvotes: 0

Views: 217

Answers (1)

jfriend00
jfriend00

Reputation: 707158

You haven't explained the output you want so here's a logical guess based on the data in your jsFiddle.

OK, here's the data from your jsFiddle in an understandable form:

var obj = {saved: [
    {"dict":"English","terms": [
        {"term":"car","rowId":"3487","time":"1384773838069"},
        {"term":"dog","rowId":"443","time":"1384776129957"},
        {"term":"plane","rowId":"16171","time":"1384776168253"},
        {"term":"bread","rowId":"127564","time":"1384959605196"}]
     }, 
     {"dict":"French","terms": [
         {"term":"Monsieur","rowId":"934","time":"1384862250130"},
         {"term":"Croissant","rowId":"13612","time":"1384900161187"},
         {"term":"suis","rowId":"942","time":"1384900437068"}]
      }
    ]
};

I'm assuming that you want to sort each terms array separately by one of the fields in it.

Here's how you could sort it by field:

// utility function for sorting an array by a key in alpha order
function sortArrayAlpha(arr, key) {
    arr.sort(function(a, b) {
        return a[key].localeCompare(b[key]);
    });
}

// utility function for sorting an array by a key in parsed numeric order
function sortArrayNum(arr, key) {
    arr.sort(function(a, b) {
        return parseInt(a[key], 10) - parseInt(b[key], 10);
    });
}

// sort by term
var dicts = obj.saved;
// cycle through each item in the saved array
for (var i = 0; i < dicts.length; i++) {
    var terms = dicts[i].terms;
    sortArrayAlpha(terms, "term");
}

// sort by time
// cycle through each item in the saved array
for (var i = 0; i < dicts.length; i++) {
    var terms = dicts[i].terms;
    sortArrayNum(terms, "time");
}

// sort by rowId
// cycle through each item in the saved array
for (var i = 0; i < dicts.length; i++) {
    var terms = dicts[i].terms;
    sortArrayNum(terms, "rowId");
}

And, a jsFiddle that shows the output: http://jsfiddle.net/jfriend00/vH8u7/. You can see in the jsFiddle the exact output format for each sort.

Here's what sort by term looks like:

{"saved":[
    {"dict":"English","terms":[
        {"term":"bread","rowId":"127564","time":"1384959605196"},
        {"term":"car","rowId":"3487","time":"1384773838069"},
        {"term":"dog","rowId":"443","time":"1384776129957"},
        {"term":"plane","rowId":"16171","time":"1384776168253"}]},
    {"dict":"French","terms":[
        {"term":"Croissant","rowId":"13612","time":"1384900161187"},
        {"term":"Monsieur","rowId":"934","time":"1384862250130"},
        {"term":"suis","rowId":"942","time":"1384900437068"}]
    }
]}

Upvotes: 1

Related Questions