如何在JavaScript中对具有多个字段值的对象数组进行排序 [英] How to sort an array of objects with multiple field values in JavaScript

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

问题描述

我发现了一种基于以下定义的属性之一对对象数组进行排序的好方法:

I found a great method to sort an array of objects based on one of the properties as defined at:

按JavaScript中的字符串属性值排序对象数组

使用该功能可以完美地用于单一排序(在所有浏览器上),甚至可以在另一种排序中使用Google Chrome进行排序!这是EgeÖzcan对象数组的好常规例程

Using that function works perfectly for a single sort (on all browsers), and even a sort within another sort EXCEPT using Google Chrome! Here is Ege Özcan's great sort routine for arrays of objects

function dynamicSort(property) { 
    return function (a,b) {
        return (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
    }
}

使用名为Data的数组(当然,我的数组有更多对象)...

Using an array named "Data" (of course, my array has many more object pairs)...

var Data = [{Category: "Business", Value: "ABC"},{Category:"Personal", Value:"XYZ"}];

我可以得到一个合适的排序,其中订单被列为每个类别中的所有值...

I can get a proper sort where the order is listed as all the values within each category by doing this...

Data.sort(dynamicSort("Value"));
Data.sort(dynamicSort("Category"));

首先排序 Value ,然后通过 Category ,我的数组将所有值按排序顺序排列,首先列出所有业务基础值,然后列出所有基于个人的值。完善!除了在Chrome中,数据按类别正确排序,但每个类别中的值的顺序似乎相当随机。

By first sorting on Value, and then by Category, my array puts all values in sorted order with all the Business-base values listed first and then all the Personal-based values. Perfect! Except in Chrome where the data is sorted properly by category, but the order of the values within each category seems rather random.

是否有人知道更好的方法一种在Chrome中也可以使用的排序?

Does any one know of a better way to do a sort within a sort that would also work in Chrome?

推荐答案

我创建了该dynamicSort函数的多参数版本:

I created a multi-parameter version of that dynamicSort function:

function dynamicSort(property) { 
    return function (obj1,obj2) {
        return obj1[property] > obj2[property] ? 1
            : obj1[property] < obj2[property] ? -1 : 0;
    }
}

function dynamicSortMultiple() {
    /*
     * save the arguments object as it will be overwritten
     * note that arguments object is an array-like object
     * consisting of the names of the properties to sort by
     */
    var props = arguments;
    return function (obj1, obj2) {
        var i = 0, result = 0, numberOfProperties = props.length;
        /* try getting a different result from 0 (equal)
         * as long as we have extra properties to compare
         */
        while(result === 0 && i < numberOfProperties) {
            result = dynamicSort(props[i])(obj1, obj2);
            i++;
        }
        return result;
    }
}

我按如下方式创建了一个数组:

I created an array as follows:

var arr = [
    {a:"a",b:"a",c:"a"},
    {a:"b",b:"a",c:"b"},
    {a:"b",b:"a",c:"a"},
    {a:"b",b:"a",c:"b"},
    {a:"b",b:"b",c:"a"},
    {a:"b",b:"b",c:"b"},
    {a:"b",b:"b",c:"a"},
    {a:"b",b:"b",c:"b"},
    {a:"b",b:"b",c:"a"},
    {a:"b",b:"b",c:"b"},
    {a:"b",b:"b",c:"a"},
    {a:"c",b:"b",c:"b"},
    {a:"c",b:"c",c:"a"}
];

当我这样做时它起作用了,

and it worked when I did,

arr.sort(dynamicSortMultiple("c","b","a"));

这是一个有效的例子: http://jsfiddle.net/ZXedp/

And here is a working example: http://jsfiddle.net/ZXedp/

这篇关于如何在JavaScript中对具有多个字段值的对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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