排序Javascript对象

时间:2012-08-15 15:24:15

标签: javascript arrays json sorting

  

可能重复:
  Sorting JavaScript Object by property value
  Sort JavaScript object by key

我编写了一个名为frequency()的函数,它接受一个数组,计算在其中找到一个字符串的次数,然后根据这些结果返回一个对象。

代码:

var array = ["Diesel","Asos","Diesel","Paul Smith"];

function frequency(array) {
    var frequency = {}, value;
    for(var i = 0; i < array.length; i++) {
        value = array[i];
        if(value in frequency)
            frequency[value]++;
        else
            frequency[value] = 1;
    }
    return frequency;
}

ordered = frequency(array);

示例输出:

{"Asos":1,"Diesel":2,"Paul Smith":1}

我现在如何根据计算的频率值对输出的物体进行排序?

0 个答案:

没有答案