简体   繁体   English

如何在Javascript中对关联数组进行排序?

[英]How to sort associative array in Javascript?

I need to sort associative array by JS for one of my projects. 我需要通过JS为我的一个项目排序关联数组。 I found this function, that works great in firefox, but unfortunately it doesnt work in IE8, OPERA, CHROME... Cant find the way to make it work in other browsers, or find another function that would fit the purpose. 我发现这个函数在firefox中运行得很好,但不幸的是它在IE8,OPERA,CHROME中无法工作......无法找到使其在其他浏览器中工作的方法,或者找到适合其目的的另一个函数。 I really appreciate any help. 我非常感谢任何帮助。

function sortAssoc(aInput)
{
    var aTemp = [];
    for (var sKey in aInput) aTemp.push([sKey, aInput[sKey].length]);
    aTemp.sort(function () {return arguments[0][1] < arguments[1][1]});
    var aOutput = new Object();
    //for (var nIndex = aTemp.length-1; nIndex >=0; nIndex--)
    for (var nIndex = 0; nIndex <= aTemp.length-1; nIndex++)
        aOutput[aTemp[nIndex][0]] = aInput[aTemp[nIndex][0]];
    //aOutput[aTemp[nIndex][0]] = aTemp[nIndex][1];
    return aOutput;
}

This is impossible. 这是不可能的。 An Object in JavaScript (which is what you're using as your "associative array") is specified as having no defined order when iterating over its properties using a for...in loop. JavaScript中的Object (您正在使用的“关联数组”)被指定为在使用for...in循环迭代其属性时没有定义的顺序 You may be able to observe some common ground between some browsers' behaviour, but it's not universal . 您可能能够在某些浏览器的行为之间观察到一些共同点,但它并不普遍

Summary: if you need objects in a specific order, use an array. 简介:如果您需要特定顺序的对象,请使用数组。

I know it's an old post but that work : 我知道这是一个老帖子但是工作:

problem is 问题是

aTemp.sort(function () {return arguments[0][1] < arguments[1][1]});

because the sort function attends a number : 因为sort函数出现一个数字:

aTemp.sort(function (a, b) {
    if (a[1] < b[1])
        return 1;
    else if (a[1] > b[1])
        return -1;
    else
        return 0;
});

I recently ran into this problem and found this question. 我最近遇到了这个问题,发现了这个问题。 I was disappointed to find that there was no pre-defined way to sort an associative array, but it made sense and pointed me in the right direction. 我很失望地发现没有预先定义的方法来对关联数组进行排序,但它有意义并指出了我正确的方向。 I didn't fully realize that in JS associative arrays are really objects and are only arrays in name. 我没有完全意识到JS关联数组实际上是对象,只是名称中的数组。 I had an associative array that contained more associative arrays, ex: 我有一个包含更多关联数组的关联数组,例如:

var firstChild = {'id': 0, 'name': 'company Two'};
var secondChild = {'id': 1, 'name': 'company One'};
var parent = {
    'company Two': firstChild,
    'company One': secondChild
};

The following function will sort the above parent array based upon it's keys. 以下函数将根据它的键对上面的父数组进行排序。 For this to work as written, the parent array needs keys that match a value in the associated array. 为了使其按写入方式工作,父数组需要与关联数组中的值匹配的键。 For instance, parent['unique string'] will need to have some key value that holds a value of 'unique string'. 例如,parent ['unique string']将需要一些包含'unique string'值的键值。 In my case, this is the name key; 就我而言,这是名称密钥; however, you can choose any key you like. 但是,您可以选择任何您喜欢的键。

function associativeSort(givenArray, keyToSort) {
    var results = [];

    var temp = [];
    for(var key in givenArray) {
        temp.push(givenArray[key].name);
    }
    temp = temp.sort();
    for(var x = 0; x < temp.length; x++) {
        results[x] = givenArray[temp[x]];
    }

    return results;
}

Given my example array, this function would return: 给定我的示例数组,此函数将返回:

var parent = {
    'company One': {'id': 1, 'name': 'company One'},
    'company Two': {'id': 0, 'name': 'company Two'}
};

It's a simple solution, but it took me a while to think of. 这是一个简单的解决方案,但我花了一段时间才想到。 Hope this helps others facing this problem. 希望这有助于其他人面对这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM