Javascript 对对象进行排序

Javascript 对对象进行排序,javascript,sorting,Javascript,Sorting,我试图订购一个从JSON“转换”的对象。 我正在尝试使用我在上找到的此函数,但它在我的情况下似乎不起作用: function sortJsonArrayByProperty(objArray, prop, direction){ if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments"); var direct = arguments.length>2 ? ar

我试图订购一个从JSON“转换”的对象。 我正在尝试使用我在上找到的此函数,但它在我的情况下似乎不起作用:

function sortJsonArrayByProperty(objArray, prop, direction){
    if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments");
    var direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending

    if (objArray && objArray.constructor===Array){
        var propPath = (prop.constructor===Array) ? prop : prop.split(".");
        objArray.sort(function(a,b){
            for (var p in propPath){
                if (a[propPath[p]] && b[propPath[p]]){
                    a = a[propPath[p]];
                    b = b[propPath[p]];
                }
            }
            // convert numeric strings to integers
            a = a.match(/^\d+$/) ? +a : a;
            b = b.match(/^\d+$/) ? +b : b;
            return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) );
        });
    }
}
函数sortjsonarrayproperty(对象、属性、方向){
if(arguments.length2?arguments[2]:1;//默认为升序
if(objArray&&objArray.constructor==数组){
变量propPath=(prop.constructor==数组)?prop:prop.split(“.”);
排序(函数(a,b){
for(propPath中的var p){
if(a[propPath[p]]和&b[propPath[p]]{
a=a[propPath[p]];
b=b[propPath[p]];
}
}
//将数字字符串转换为整数
a=a.match(/^\d+$/)?+a:a;
b=b.match(/^\d+$/)?+b:b;
返回((ab)?1*直接:0));
});
}
}
这是我的

我想以3种不同的方式对其进行排序:

  • 按dict按时间顺序排列(使用“时间”字段)
  • 按dict字母顺序排列(使用“术语”字段)
  • 所有按时间顺序排列(忽略dict分隔)

如果这个对象不是嵌套的,我就知道怎么做了。我可以调整这个函数吗?

你还没有解释你想要的输出,所以这里有一个基于JSFIDLE中数据的逻辑猜测

好的,以下是来自JSFIDLE的数据,格式可以理解:

var obj = {saved: [
    {"dict":"English","terms": [
        {"term":"car","rowId":"3487","time":"1384773838069"},
        {"term":"dog","rowId":"443","time":"1384776129957"},
        {"term":"plane","rowId":"16171","time":"1384776168253"},
        {"term":"bread","rowId":"127564","time":"1384959605196"}]
     }, 
     {"dict":"French","terms": [
         {"term":"Monsieur","rowId":"934","time":"1384862250130"},
         {"term":"Croissant","rowId":"13612","time":"1384900161187"},
         {"term":"suis","rowId":"942","time":"1384900437068"}]
      }
    ]
};
我假设您希望按照每个术语数组中的一个字段分别对其进行排序

以下是如何按字段对其进行排序:

// utility function for sorting an array by a key in alpha order
function sortArrayAlpha(arr, key) {
    arr.sort(function(a, b) {
        return a[key].localeCompare(b[key]);
    });
}

// utility function for sorting an array by a key in parsed numeric order
function sortArrayNum(arr, key) {
    arr.sort(function(a, b) {
        return parseInt(a[key], 10) - parseInt(b[key], 10);
    });
}

// sort by term
var dicts = obj.saved;
// cycle through each item in the saved array
for (var i = 0; i < dicts.length; i++) {
    var terms = dicts[i].terms;
    sortArrayAlpha(terms, "term");
}

// sort by time
// cycle through each item in the saved array
for (var i = 0; i < dicts.length; i++) {
    var terms = dicts[i].terms;
    sortArrayNum(terms, "time");
}

// sort by rowId
// cycle through each item in the saved array
for (var i = 0; i < dicts.length; i++) {
    var terms = dicts[i].terms;
    sortArrayNum(terms, "rowId");
}

不要将整个对象传递给
sortjsonarrayproperty
,而是独立地传递每个
术语
数组。这是最后一种组合两个数组的方法。请备份,而不是发布找到并尝试过的一段代码,描述您实际试图解决的问题。向我们展示您的数据以及您希望排序后的输出是什么样子。这是我们能够正确帮助您的唯一方法。请在您的实际问题中发布这些信息,而不是在JSFIDLE中。在查看JSFIDLE中的数据时,不清楚您希望排序的确切内容,因为其中有多个数组。FIDLE中有一个obj副本。@Andy-这是完全不清楚他们希望输出是什么样子。JSFIDLE中有多个数组。例如,有多个
术语
数组。此外,问题应该是独立的,不必看JSFIDLE就可以理解问题的含义。是的,我还没有意识到obj是如何嵌套的。。。
{"saved":[
    {"dict":"English","terms":[
        {"term":"bread","rowId":"127564","time":"1384959605196"},
        {"term":"car","rowId":"3487","time":"1384773838069"},
        {"term":"dog","rowId":"443","time":"1384776129957"},
        {"term":"plane","rowId":"16171","time":"1384776168253"}]},
    {"dict":"French","terms":[
        {"term":"Croissant","rowId":"13612","time":"1384900161187"},
        {"term":"Monsieur","rowId":"934","time":"1384862250130"},
        {"term":"suis","rowId":"942","time":"1384900437068"}]
    }
]}