gpt4 book ai didi

javascript - 如何在 JavaScript 中进行字母数字排序

转载 作者:行者123 更新时间:2023-11-28 02:12:04 25 4
gpt4 key购买 nike

这是我的 JavaScript 代码,用于以字母数字方式对数据进行排序:

var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;
function sortAlphaNum(a,b) {
var aA = a.replace(reA, "");
var bA = b.replace(reA, "");
if(aA === bA) {
var aN = parseInt(a.replace(reN, ""), 10);
var bN = parseInt(b.replace(reN, ""), 10);
return aN === bN ? 0 : aN > bN ? 1 : -1;
} else {
return aA > bA ? 1 : -1;
}
}

我的输入数据:

["ap1", "ap4", "ap12","4ggh2","7ggh9","9tggfg2","4gghdd2","4gghfg2"]

排序后应该是这样的:

4ggh2,4hghdd2,4kghfg2,7ggh9,9tggfg2,ap1,ap4,ap12 

这并没有发生,你能帮我解决这个问题吗?

function sortAlphaNum(a, b) { 
var x = a.split("/");
var y = b.split("/");
x = x[x.length-1].replace(/\\\s/g," ").split(/(\d )/);
y = y[y.length-1].replace(/\\\s/g," ").split(/(\d )/);
for (var i in x) {
if (x[i] && !y[i] || isFinite(x[i]) && !isFinite(y[i])) {
return -1;
} else if (!x[i] && y[i] || !isFinite(y[i]) && isFinite(y[i])) {
return 1;
} else if (!isFinite(x[i]) && !isFinite(y[i])) {
x[i] = x[i].toLowerCase();
y[i] = y[i].toLowerCase();
if (x[i] < y[i]) return -1;
if (x[i] > y[i]) return 1;
} else {
x[i] = parseFloat(x[i]);
y[i] = parseFloat(y[i]);
if (x[i] < y[i]) return -1;
if (x[i] > y[i]) return 1;
}
}
return 0;
}
alert(["ap1", "ap4", "ap12","4ggh2","7ggh9","9tggfg2","4hghdd2","4kghfg2"].sort(sortAlphaNum));

最佳答案

此示例向数组原型(prototype)添加一个方法,

但如果您愿意,可以将其编写为独立函数,并将要排序的数组作为参数传递。

Array.prototype.naturalSort= function(index){
var T= this, L= T.length, i, who, next,
isi= typeof index== 'number',
rx=/(\.\d+)|(\d+(\.\d+)?)|([^\d.]+)|(\.\D+)|(\.$)/g;
function nSort(aa, bb){
var a= aa[0], b= bb[0], a1, b1, i= 0, n, L= a.length;
while(i<L){
if(!b[i]) return 1;
a1= a[i];
b1= b[i++];
if(a1!== b1){
n= a1-b1;
if(!isNaN(n)) return n;
return a1>b1? 1:-1;
}
}
return b[i]? -1:0;
}
for(i= 0; i<L; i++){
who= T[i];
next= isi? T[i][index] || '':who;
T[i]= [String(next).toLowerCase().match(rx), who];
}
T.sort(nSort);
for(i= 0; i<L; i++){
T[i]= T[i][1];
}
return this;
}
var A= ["ap1","ap4","ap12","4ggh2","7ggh9","9tggfg2","4gghdd2","4gghfg2"];

A.naturalSort();

//返回值:(数组) //4ggh2,4gghdd2,4gghfg2,7ggh9,9tggfg2,ap1,ap4,ap12

  1. The example first goes through the entire array and sets each item to a new two-item array- the first item of the new array is a matched array of numbers and strings, the second is the original value.

In your example, array[5]= '9tggfg2', becomes array[5]= [['9', 'tggfg', '2'], '9tggfg2']]. That way the reg exp, match and and toLowerCase operations are only done once for each item, rather than every time the sort compares two items.

  1. After the array is prepped, the sort function is applied, which compares items alphabetically if they are both alpha-strings and numerically if they are both number-strings. Number-strings sort before(are less-than) alpha-strings.

  2. After the sort, the array is again processed, this time to reset each item to its original value.

您可以使用相同的逻辑,而无需预排序和后排序部分,但如果您有超过三个项目,则需要更长的时间:

function natSort(as, bs){
var a, b, a1, b1, i= 0, n, L,
rx=/(\.\d+)|(\d+(\.\d+)?)|([^\d.]+)|(\.\D+)|(\.$)/g;
a= String(as).toLowerCase().match(rx);
b= String(bs).toLowerCase().match(rx);
if(as=== bs) return 0;
L= a.length;
while(i<L){
if(!b[i]) return 1;
a1= a[i],
b1= b[i++];
if(a1!== b1){
n= a1-b1;
if(!isNaN(n)) return n;
return a1>b1? 1:-1;
}
}
return b[i]? -1:0;
}
["ap1", "ap4", "ap12", "4ggh2", "7ggh9",
"9tggfg2", "4gghdd2", "4gghfg2"].sort(natSort);

返回值:(数组) 4ggh2,4gghdd2,4gghfg2,7ggh9,9tggfg2,ap1,ap4,ap12

关于javascript - 如何在 JavaScript 中进行字母数字排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16938965/

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