這是我的數組,我試圖對它進行排序。但它沒有按預期工作。我想按降序對名稱進行排序..我該怎麼做?jquery數組排序不按預期工作
var d = '{"success":"1","message":[{"_id":"591b39df358f1d1f843231d1","area":"chennai","food":"idly","name":"saravana bavan","__v":0},{"_id":"591b39e0358f1d1f843231d2","area":"Dindigul","food":"Dosa","name":"Kattu Briyani","__v":0},{"_id":"591b39df358f1d1f843231d4","area":"Tirupur","food":"Poori","name":"French Loaf","__v":0}]}';
console.log(d);
var results = jQuery.parseJSON(d);
console.log(results.message);
results.message.sort(function(a, b) {
return b.name- a.name;
});
console.log(results.message);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
這是我Fiddle
您可以使用SO具有演示'<>' – guradio
如果你想的名字降序排序,那麼你爲什麼排序按面積? – Bergi
@Bergi,抱歉,這是一個錯字,相應地改變它 –