Javascript 自然排序对象

标签 javascript arrays sorting object

我有一个像这样的对象数组

array[0] = {id: 1, name: "First"}
array[1] = {id: 2, name: "Second"}

我尝试使用以下方法按 name 字段排序:

Array.prototype.alphanumSort = function (caseInsensitive) {
for (var z = 0, t; t = ((typeof this[z] == "string" || typeof this[z] == "undefined") ? this[z] : this[z].name); z++) {
    this[z] = new Array();
    var x = 0, y = -1, n = 0, i, j;

    while (i = (j = t.charAt(x++)).charCodeAt(0)) {
        var m = (i == 46 || (i >= 48 && i <= 57));
        if (m !== n) {
            this[z][++y] = "";
            n = m;
        }
        this[z][y] += j;
    }
}

this.sort(function (a, b) {
    for (var x = 0, aa, bb; (aa = a[x]) && (bb = b[x]); x++) {
        if (caseInsensitive) {
            aa = aa.toLowerCase();
            bb = bb.toLowerCase();
        }
        if (aa !== bb) {
            var c = Number(aa), d = Number(bb);
            if (c == aa && d == bb) {
                return c - d;
            } else return (aa > bb) ? 1 : -1;
        }
    }
    return a.length - b.length;
});

for (var z = 0; z < this.length; z++)
    this[z] = this[z].join("");
}

但我从中返回的列表包含一个数组,其中只有 name 字段,没有相应的 id。为什么会这样? 有没有办法应用这个算法来过滤对象? 我是 JavaScript 的新手,所以请保持温柔。

最佳答案

你可以使用 sorting with mapString#localeCompare带有选项

sensitivity

Which differences in the strings should lead to non-zero result values. Possible values are:

  • "base": Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A.
  • "accent": Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples: a ≠ b, a ≠ á, a = A.
  • "case": Only strings that differ in base letters or case compare as unequal. Examples: a ≠ b, a = á, a ≠ A.
  • "variant": Strings that differ in base letters, accents and other diacritic marks, or case compare as unequal. Other differences may also be taken into consideration. Examples: a ≠ b, a ≠ á, a ≠ A.

The default is "variant" for usage "sort"; it's locale dependent for usage "search".

numeric

Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are true and false; the default is false. This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence. Implementations are not required to support this property.

var array = ['Ex 10', 'Ex 2', 'a 10.1', 'a 1.1', 'a 10.0', 'a 2.0'];

array.sort((a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }));

console.log(array)

关于Javascript 自然排序对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52660451/

相关文章:

python - 如何用numpy制作沿轴的Hadamard积?

php - MySQL 排序方式,然后对选择进行排序

java - Java 中整数数组的排序和比较

javascript - 我如何在 JavaScript 中对这个数组进行排序?

javascript - 这在 devtools 中是未定义的,即使这不是未定义的

javascript - 设备运动事件未在从跨源 iframe chrome 62 打开的选项卡中触发

c++ - 如何根据其值的某些转换的值对范围进行排序?

php - PrestaShop 对类别中的多个字段进行排序

javascript - 只允许输入 float

javascript - 适用于 Chrome 但不适用于 Internet Explorer