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

发布于 2024-09-29 11:58:27 字数 653 浏览 1 评论 0原文

我需要为我的一个项目通过 JS 对关联数组进行排序。我发现这个函数在 Firefox 中运行得很好,但不幸的是它在 IE8、OPERA、CHROME 中不起作用...无法找到使其在其他浏览器中运行的方法,或者找到另一个适合该目的的函数。我真的很感谢任何帮助。

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

I need to sort associative array by JS for one of my projects. 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. 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春如此纠结 2024-10-06 11:58:27

这是不可能的。 JavaScript 中的 Object (即您用作“关联数组”的对象)是 使用 for...in 循环迭代其属性时指定为没有定义的顺序。您也许能够观察到某些浏览器行为之间的一些共同点,但是它是不通用

摘要:如果您需要按特定顺序排列对象,请使用数组。

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. 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.

最美的太阳 2024-10-06 11:58:27

我知道这是一篇旧文章,但有效:

问题是

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

因为排序函数涉及一个数字:

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

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 :

aTemp.sort(function (a, b) {
    if (a[1] < b[1])
        return 1;
    else if (a[1] > b[1])
        return -1;
    else
        return 0;
});
蓝天白云 2024-10-06 11:58:27

我最近遇到了这个问题并发现了这个问题。我很失望地发现没有预定义的方法来对关联数组进行排序,但它很有意义,并为我指明了正确的方向。我没有完全意识到在 JS 中关联数组实际上是对象,只是名称上的数组。我有一个包含更多关联数组的关联数组,例如:

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

以下函数将根据其键对上述父数组进行排序。为了使其按照书面方式工作,父数组需要与关联数组中的值匹配的键。例如,parent['unique string'] 需要有一些保存“unique string”值的键值。就我而言,这是名称键;但是,您可以选择任何您喜欢的键。

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

给定我的示例数组,该函数将返回:

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

这是一个简单的解决方案,但我花了一段时间才想到。希望这可以帮助其他面临这个问题的人。

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. 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'. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文