我在 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;
现在我的工作是根据价格对它们进行排序。如何做到这一点?
最佳答案
根据我上面的评论,您可能应该使用对象而不是多维数组。这是一个例子(想象一下你的附加属性,如 name
和 IMage
包括在内,我没有包括在内是为了减少输入)
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' }
]
*/
关于javascript - 多维数组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9642517/