Javascript中的特殊字符排序
我有一个具有以下值的数组Javascript中的特殊字符排序,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有一个具有以下值的数组 asd sdf dsdf 1sadf *sdf !sdf @asdf _asd .sadf (sadf )sadf #sadf ^asdf &asdf %asdf -sadf =sadf +sadf -sdf 我想用javascript将其分为以下三部分 从特殊字符开始的单词 从数字开始的单词 从字母表开始的单词 所以这应该是排序数组的序列 编辑: 这是我一直在试验的一个函数: function naturalSort(a, b) { a = a.pa
asd sdf dsdf 1sadf *sdf !sdf @asdf _asd .sadf (sadf )sadf #sadf
^asdf &asdf %asdf -sadf =sadf +sadf -sdf
我想用javascript将其分为以下三部分
function naturalSort(a, b) {
a = a.path.toLowerCase();
b = b.path.toLowerCase();
var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
sre = /(^[ ]*|[ ]*|[_]*$)/g,
dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
hre = /^0x[0-9a-f]+$/i,
ore = /^0/,
// convert all to strings and trim()
x = a.toString().replace(sre, '') || '',
y = b.toString().replace(sre, '') || '',
// chunk/tokenize
xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
// numeric, hex or date detection
xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null;
// first try and sort Hex codes or Dates
if (yD)
if (xD < yD) return -1;
else if (xD > yD) return 1;
// natural sorting through split numeric strings and default strings
for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
// find floats not starting with '0', string or 0 if not defined (Clint Priest)
oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
// handle numeric vs string comparison - number < string - (Kyle Adams)
if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? -1 : 1;
// rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
else if (typeof oFxNcL !== typeof oFyNcL) {
oFxNcL += '';
oFyNcL += '';
}
if (oFxNcL <= oFyNcL) return -1;
if (oFxNcL >= oFyNcL) return 1;
}
return 0;
}
函数自然排序(a,b){
a=a.path.toLowerCase();
b=b.path.toLowerCase();
变量re=/(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$^0x[0-9a-f]+$[0-9]+)/gi,
sre=/(^[]*.[]*.[\]*$)/g,
dre=/(^([\w]+,?[\w]+)?[\w]+,?[\w]+\d+:\d+(:\d+)[\w]?^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}\w+,\w+\d}/,,
hre=/^0x[0-9a-f]+$/i,
ore=/^0/,,
//将所有字符串转换为字符串并修剪()
x=a.toString().替换(sre,,)| |,
y=b.toString().替换(sre,,)| |“”,
//区块/标记化
xN=x.replace(re,'\0$1\0')。replace(/\0$/,'')。replace(/^\0/,'')。split('\0'),
yN=y.replace(re,'\0$1\0')。replace(/\0$/,'')。replace(/^\0/,'')。split('\0'),
//数字、十六进制或日期检测
xD=parseInt(x.match(hre))| |(xN.length!=1&&x.match(dre)&&Date.parse(x)),
yD=parseInt(y.match(hre))| | xD&&y.match(dre)&&Date.parse(y)| | null;
//首先尝试对十六进制代码或日期进行排序
if(yD)
如果(xDyD)返回1;
//通过拆分数字字符串和默认字符串进行自然排序
for(var cLoc=0,numS=Math.max(xN.length,yN.length);cLoc
老实说,我不知道你发布的函数是做什么的。。。一点也不
下面的方法使用位置引用比较字符串的第一个字符。第一个字符相同的字符串会定期排序
顺便说一句,没有测试空字符串
function MySort(alphabet)
{
return function(a, b) {
var index_a = alphabet.indexOf(a[0]),
index_b = alphabet.indexOf(b[0]);
if (index_a === index_b) {
// same first character, sort regular
if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
return 0;
} else {
return index_a - index_b;
}
}
}
var items = ['asd','sdf', 'dsdf', '1sadf', '*sdf', '!sdf', '@asdf', '_asd', '.sadf', '(sadf', ')sadf', '#sadf', '^asdf', '&asdf', '%asdf', '-sadf', '=sadf', '+sadf', '-sdf', 'sef'],
sorter = MySort('*!@_.()#^&%-=+01234567989abcdefghijklmnopqrstuvwxyz');
console.log(items.sort(sorter));
这也可以起作用:
函数排序(a,b){
常量digitRegex=/^\d/;
常量alphabetRegex=/^[a-zA-Z]/;
常量symbolRegex=/^[^\w\s]/;
常量分数a=symbolRegex.测试(a)*1 | |数字测试(a)*10 | |字母测试(a)*100;
常量分数b=符号规则测试(b)*1 | |数字规则测试(b)*10 | |字母规则测试(b)*100;
如果(分数A!==分数B){
返回scoreA-scoreB;
}
if(ab){
返回1;
}
返回0;
}
常量a=['def'、'%rec'、'456'、'^we'、'123'、'abc'].sort(sortArray);
控制台日志(a)代码>两个简单的解决方案:
使用Intl.Collator
myArray.sort(Intl.Collator().compare)
使用localeCompare
myArray.sort((a, b) => a.localeCompare(b))
Intl.Collator更为出色…到目前为止,您尝试了什么?尝试一下,没有那么难。所以在数字之前是特殊字符。但是@
在
之前吗?角色的顺序是什么?您不能想要ASCII顺序,因为在ASCII中,=
在数字之后@拉维,你需要证明你至少试过什么。努力在这里是有价值的。我已经尝试过这个功能。。函数(a,b){返回a>b?1:(a@JonathanM我能理解,但我已经尝试了很多,却没有得到正确的答案!嘿,杰克…你是个伟大的人…它解决了我的问题…谢谢你,伙计…你真是太棒了。。。!
myArray.sort((a, b) => a.localeCompare(b))