所以我想我的排序阵列,基于对象的值对象..使用Javascript - 排序与对象数组基于对象的价值
var mostCheapHistory [ { lowestTitle: title goes here, lowestPrice: 100 }, another obj, another, another } ]
而且我希望用他们的价格对它们进行排序,所以我想出了这个:
var historyObj = mostCheapHistory[0];
for(var y in mostCheapHistory){
nextObj = mostCheapHistory[y];
console.log('Is '+ historyObj.lowestPrice + ' more as ' + nextObj.lowestPrice + ' ?');
console.log(historyObj.lowestPrice > nextObj.lowestPrice);
console.log('-----')
}
这是输出...
Is 124.98 more as 124.98 ?
false
-----
Is 124.98 more as 18.59 ?
false
-----
Is 124.98 more as 25.9 ?
false
-----
Is 124.98 more as 26.99 ?
false
-----
Is 124.98 more as 34.76 ?
false
-----
这到底是怎么回事?很明显,124.98更像34.76,但它是假的?
for(var x=0; x < items.length; x++){
var title = items[x].title[0];
var price = items[x].sellingStatus[0].currentPrice[0].__value__;
var item =
{
lowestPrice : price,
lowestTitle : title
}
if(x === 0){
mostCheapHistory.push(item);
}
else{
for(var y=0; y < mostCheapHistory.length; y++){
if(mostCheapHistory[y].lowestPrice > price){
if(mostCheapHistory.length < 5){
mostCheapHistory.push(item);
break;
}
else{
mostCheapHistory[y] = item;
break;
}
}
}
}
}
请加分类部分。 –
它不排序任何东西,因为我真的不能测试,如果一个值越大,作为另一个你可以看到 –
请加阵列的真实数据。 –