javascript - 多维数组排序

标签 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;

现在我的工作是根据价格对它们进行排序。如何做到这一点?

最佳答案

根据我上面的评论,您可能应该使用对象而不是多维数组。这是一个例子(想象一下你的附加属性,如 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' } 
    ]
*/

关于javascript - 多维数组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9642517/

相关文章:

javascript - onHashChange 在 Safari 中有效吗?

javascript - 在 Teoria.js 中反转和弦的规范方法是什么

javascript - 分配数组会导致某些数据出现语法错误

algorithm - 对包含随机数的数组进行排序

list - 如何逆转Groovy集合的种类?

javascript - 箭头函数不应该返回赋值吗?

javascript - Firefox 和 Chrome 的 CORS 错误

php - 如何使用php将数组值存储到mysql表中的单个单元格中?

arrays - 如何将对象从 pickerView append 到数组

python - list.sort(key=list.count) 在 Python 3.x 中如何工作?