2

私は次のような配列を持っています:

data = 
[
    {name: "Monday", amount: 67, colour: "red"},

    {name: "Tuesday", amount: 23, colour: "blue"},

    {name: "Wednesday", amount: 50, colour: "yellow"},

    {name: "Thursday", amount: 70, colour: "green"},

    {name: "Friday", amount: 20, colour: "orange"},

    {name: "Saturday", amount: 90, colour: "black"},

    {name: "Sunday", amount: 10, colour: "brown"}
]

配列を「amount」値の降順で並べ替えたいので、並べ替えると、配列は次のようになります。

data = 
[
    {name: "Saturday", amount: 90, colour: "black"},

    {name: "Thursday", amount: 70, colour: "green"},

    {name: "Monday", amount: 67, colour: "red"},

    {name: "Wednesday", amount: 50, colour: "yellow"},

    {name: "Tuesday", amount: 23, colour: "blue"},

    {name: "Friday", amount: 20, colour: "orange"},

    {name: "Sunday", amount: 10, colour: "brown"}
]

[0], [1], [2]私はstackoverflowでこれに似たさまざまな投稿を見てきましたが、これらの問題を、単なるインデックス(名前、量、色)などではなく名前を付けた私のようなものに解釈するのは難しいと思います。

4

1 に答える 1

3

Array.sortを使用します:

data.sort(function(a, b) {
    return b.amount - a.amount;
});
于 2013-03-02T16:04:05.190 に答える