多维数组排序

Pra*_*ngh 1 javascript arrays sorting

我在javascript中有一个多维数组定义为: -

myArray[0][0] = Some IMage;
myArray[0][1] = Some price;
myArray[0][2] = Some name;
myArray[0][3] = Some values;
myArray[0][4] = Some otherValues;
myArray[1][0] = Some IMage;
myArray[1][1] = Some price;
myArray[1][2] = Some name;
myArray[1][3] = Some values;
myArray[1][4] = Some otherValues;
Run Code Online (Sandbox Code Playgroud)

现在我的工作是根据价格对它们进行分类.如何才能做到这一点 ?

Mar*_*all 5

根据我上面的评论,您应该使用对象而不是多维数组.下面是一个例子(像想象你的附加属性nameIMage包括在内,这我不包括少打字的缘故)

var arr = [
    { price: 12, something: 'a b c' },
    { price: 8, something: 'a b c' },
    { price: 45, something: 'a b c' },
    { price: 10, something: 'a b c' }
];

arr.sort(function(a, b) { return a.price - b.price; });

/*
    arr is now:

    [ 
        { price: 8, something: 'a b c' },
        { price: 10, something: 'a b c' },
        { price: 12, something: 'a b c' },
        { price: 45, something: 'a b c' } 
    ]
*/
Run Code Online (Sandbox Code Playgroud)