按属性值对对象数组进行排序

a 夏天 提交于 2020-08-14 22:58:17

问题:

I've got the following objects using AJAX and stored them in an array: 我使用AJAX获得了以下对象并将它们存储在数组中:

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"
    }
];

How do I create a function to sort the objects by the price property in ascending or descending order using JavaScript only? 如何创建仅使用JavaScript price属性按升序 降序对对象进行排序的函数?


解决方案:

参考一: https://stackoom.com/question/46kS/按属性值对对象数组进行排序
参考二: https://oldbug.net/q/46kS/Sorting-an-array-of-objects-by-property-values
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!