gpt4 book ai didi

javascript - 在javascript中按多个键和多个顺序排序

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:45:54 25 4
gpt4 key购买 nike

我需要对对象数组进行排序,提供带有键及其顺序的字典。就像这样:

var data = [
{ "name": "a", "age": 1, "money": 4 },
{ "name": "f", "age": 4, "money": 1 },
{ "name": "c", "age": 2, "money": 3 },
{ "name": "a", "age": 0, "money": 1},
{ "name": "f", "age": 4, "money": 3 },
{ "name": "c", "age": 1, "money": 4 },
{ "name": "c", "age": 3, "money": 1 }
];

var data = data.multiSort({
name: "asc",
age: "desc",
money: "desc"
});

console.log(data);
/*
{ "name": "a", "age": 1, "money": 4 },
{ "name": "a", "age": 0, "money": 1},
{ "name": "c", "age": 3, "money": 1 }
{ "name": "c", "age": 2, "money": 3 },
{ "name": "c", "age": 1, "money": 4 },
{ "name": "f", "age": 4, "money": 3 },
{ "name": "f", "age": 4, "money": 1 }
*/

我完全卡住了,不知道如何实现。很多人指出这段简单的代码,但我不明白它应该如何实现我想要做的事情。 https://github.com/Teun/thenBy.js

这是我现在拥有的代码。我知道我离解决方案还很远,但是如果我能帮助您了解如何实现这一目标,我将不胜感激,因为我需要在 javascript 方面进行大量改进。

Array.prototype.multiSort = function(sorters){

function getNextSorter(sorters, currentSorterKey) {
var sortersKeys = Object.keys(sorters);
if(!currentSorterKey)
currentSorterIndex = 0;
else
currentSorterIndex = sortersKeys.indexOf(currentSorterKey) + 1;

var key = sortersKeys[currentSorterIndex];
var order = sorters[key];
}

function compare(a, b, key, order) {
var a = a[key];
var b = b[key];

//if both numbers compare them as numbers and not as strings
var numericA = parseFloat(a);
var numericB = parseFloat(b);
if(!isNaN(numericA) && !isNaN(numericB)) {
a = numericA;
b = numericB;
}

//if different compare them with the given order
if(a != b)
return (order == "asc") ? a - b : b - a;
//else compare next key as specified in sorters (if next key is present!)
else
//how to recursively call to get the next compare function?
}

//how to call sort now?

return this;
};

最佳答案

要使这项工作可靠,您将不得不更改接收数据的方式。一个对象不保证枚举的顺序,因此它可能看起来工作了一段时间,但它随时可能失败。

而是使用其他格式。下面我将使用带有 : 分隔符的字符串。像这样:

Array.prototype.multiSort = function() {
// Make an array of arrays:
// [ ["name" :"asc" ],
// ["age" :"desc"],
// ["money":"desc"] ]
var sorters = Array.prototype.map.call(arguments, function(s) {
return s.split(":");
});

function compare(a, b) {
// Iterate over the sorters, and compare using the first non-equal values.
for (var i = 0; i < sorters.length; i++) {
var a_val = a[sorters[i][0]];
var b_val = b[sorters[i][0]];

if (a_val === b_val) {
continue; // They're equal values, so try the next sorter
}
// Swap values if not ascending
if (sorters[i][1] !== "asc") {
var temp = a_val;
a_val = b_val;
b_val = temp;
}
// Use `.localeCompare()` if they're both strings
if (typeof a_val === "string" && typeof b_val === "string") {
return a_val.localeCompare(b_val);
}
return a_val - b_val;
}
}

return this.sort(compare);
};

var data = [
{ "name": "a", "age": 1, "money": 4 },
{ "name": "f", "age": 4, "money": 1 },
{ "name": "c", "age": 2, "money": 3 },
{ "name": "a", "age": 0, "money": 1 },
{ "name": "f", "age": 4, "money": 3 },
{ "name": "c", "age": 1, "money": 4 },
{ "name": "c", "age": 3, "money": 1 }
];

var data = data.multiSort("name:asc", "age:desc", "money:desc");

document.body.innerHTML = "<pre>" + JSON.stringify(data, null, 4) + "</pre>";

所以它的作用是拆分 : 上的每个字符串,以便我们得到一个结果数组,其中包含要排序的键和方向。

然后在compare 函数中,我们只是遍历sorters,从ab 中获取值>,如果它们不相等,我们继续返回比较结果。如果它们相等,我们需要转到下一个排序器并尝试使用那个排序器。

所以没有真正需要额外的功能。 for 循环就是所需的全部内容。

关于javascript - 在javascript中按多个键和多个顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27363952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com