3

JavaScript オブジェクトの配列があります。

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
    }
}
];

wallet.cash で並べ替えるにはどうすればよいですか?

次の例 (Ege Özcan による) は、これらのオブジェクトを名前または年齢で並べ替えたい場合に機能しますが、多次元キーで機能するように変更するのは困難です。

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;
}
}

これは機能します:

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

これはしません:

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

前もって感謝します!

4

3 に答える 3

2

2 つの異なる引数を dynamicSort 関数に渡すことができます

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;
}
}

そして、電話する

dynamicSort("wallet","cash");

デモ

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"))))

于 2015-06-17T09:29:24.913 に答える
0

このスタックオーバーフローの回答Object.byStringのメソッドを使用して、次のようにコードを変更できます。

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;
    }
}
于 2015-06-17T09:49:46.113 に答える
0

hereからこの比較関数を持ち上げると、次のことができます。

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));
于 2015-06-17T09:28:54.123 に答える