Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have an array of JavaScript objects:

var people = [
{
    "name": "Edward",
    "age": 100,
    "wallet": {
        "location": "home",
        "cash": 500
    },
    "bank": {
        "location": "bank street",
        "cash": 22100
    }
},
{
    "name": "Lisa",
    "age": 30,
    "wallet": {
        "location": "home",
        "cash": 20
    },
    "bank": {
        "location": "bank street",
        "cash": 12340
    }
},
{
    "name": "Elisabeth",
    "age": 50,
    "wallet": {
        "location": "home",
        "cash": 200
    },
    "bank": {
        "location": "bank street",
        "cash": 5000
    }
}
];

How can I sort them by wallet.cash ?

This following example (by Ege Özcan) works if I wanted to sort these objects by name, or age, but I have hard time modifying it to work with multidimensional key.

function dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
    sortOrder = -1;
    property = property.substr(1);
}
return function (a,b) {
    var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
    return result * sortOrder;
}
}

This works:

people.sort(dynamicSort("name"));

This doesn't:

people.sort(dynamicSort("wallet.cash"));

Thank you in advance!

share|improve this question

3 Answers 3

up vote 0 down vote accepted

You can pass two different arguments to dynamicSort function

function dynamicSort(property1,property2) {
var sortOrder = 1;
if(property1[0] === "-") {
    sortOrder = -1;
    property1 = property1.substr(1);
}
return function (a,b) {
    var result = (a[property1][property2] < b[property1][property2]) ? -1 : (a[property1][property2] > b[property1][property2]) ? 1 : 0;
    return result * sortOrder;
}
}

And then call

dynamicSort("wallet","cash");

DEMO

var people = [
{
    "name": "Edward",
    "age": 100,
    "wallet": {
        "location": "home",
        "cash": 500
    },
    "bank": {
        "location": "bank street",
        "cash": 22100
    }
},
{
    "name": "Lisa",
    "age": 30,
    "wallet": {
        "location": "home",
        "cash": 20
    },
    "bank": {
        "location": "bank street",
        "cash": 12340
    }
},
{
    "name": "Elisabeth",
    "age": 50,
    "wallet": {
        "location": "home",
        "cash": 200
    },
    "bank": {
        "location": "bank street",
        "cash": 5000
    }
}
];

    function dynamicSort(property1,property2) {
    var sortOrder = 1;
    if(property1[0] === "-") {
        sortOrder = -1;
        property1 = property1.substr(1);
    }
    return function (a,b) {
        var result = (a[property1][property2] < b[property1][property2]) ? -1 : (a[property1][property2] > b[property1][property2]) ? 1 : 0;
        return result * sortOrder;
    }
    }

alert(JSON.stringify(people.sort(dynamicSort("wallet","cash"))))

share|improve this answer
    
Thanks man! This was exactly what I was looking for. The first code part however is missing property -> property1 changes. – Beeq Jun 17 at 11:59

Lifting this comparison function from here, you can do:

function compare(a,b) {
  if (a.wallet.cash < b.wallet.cash)
    return -1;
  if (a.wallet.cash > b.wallet.cash)
    return 1;
  return 0;
}

var people = [...];
console.log(people.sort(compare));
share|improve this answer

You can use Object.byString method from this stackoverflow answer and modify your code like this:

function dynamicSort(property) {
    var sortOrder = 1;
    if(property[0] === "-") {
        sortOrder = -1;
        property = property.substr(1);
    }
    return function (a,b) {
        var avalue = Object.byString(a, property);
        var bvalue = Object.byString(b, property);
        var result = (avalue < bvalue) ? -1 : (avalue > bvalue) ? 1 : 0;
        return result * sortOrder;
    }
}
share|improve this answer

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.