排序的JavaScript对象的数组 [英] Sorting an array of JavaScript objects

查看:107
本文介绍了排序的JavaScript对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读使用Ajax的下列对象,并存储它们放入数组:

I read the following objects using Ajax and stored them in an array:

var homes = [
    {
        "h_id": "3",
        "city": "Dallas",
        "state": "TX",
        "zip": "75201",
        "price": "162500"
    }, {
        "h_id": "4",
        "city": "Bevery Hills",
        "state": "CA",
        "zip": "90210",
        "price": "319250"
    }, {
        "h_id": "5",
        "city": "New York",
        "state": "NY",
        "zip": "00010",
        "price": "962500"
    }
];

我如何创建一个函数来使用价格的的和的降序的顺序属性中的对象进行排序只有JavaScript?

How do I create a function to sort the objects by the price property in ascending and descending order using only JavaScript?

推荐答案

按价格排序家升序排列:

Sort homes by price in ascending order:

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

这篇关于排序的JavaScript对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆