在javascript中,使用数字和特殊字符对字符串进行排序
我试图用数字和特殊字符组合对字符串进行排序 但它给出了错误的顺序在javascript中,使用数字和特殊字符对字符串进行排序,javascript,sorting,Javascript,Sorting,我试图用数字和特殊字符组合对字符串进行排序 但它给出了错误的顺序 <!DOCTYPE html> <html> <body> <p>Click the button to sort the array.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> functi
<!DOCTYPE html>
<html>
<body>
<p>Click the button to sort the array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
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;
}
}
}
function myFunction() {
var items = ["AB_UI08","AB_UI03","AB_UI07","AB_UI04","AB_UI05","AB_UI014","AB_UI01","AB_UI09","AB_UI010","AB_UI011","AB_UI012","AB_UI013","AB_UI06","AB_UI016","AB_UI07","AB_UI018","AB_UI019","AB_UI015","AB_UI020","AB_UI02"
],
sorter = MySort('*!@_.()#^&%-=+01234567989abcdefghijklmnopqrstuvwxyz');
console.log(items.sort(sorter));
}
</script>
</body>
</html>
单击按钮对数组进行排序
试试看
函数MySort(字母表)
{
返回函数(a,b){
var index_a=字母表indexOf(a[0]),
index_b=字母表indexOf(b[0]);
如果(索引a==索引b){
//相同的第一个字符,按常规排序
if(ab){
返回1;
}
返回0;
}否则{
返回索引_a-索引_b;
}
}
}
函数myFunction(){
变量项=[“AB_UI08”、“AB_UI03”、“AB_UI07”、“AB_UI04”、“AB_UI05”、“AB_UI014”、“AB_UI01”、“AB_UI09”、“AB_UI010”、“AB_UI011”、“AB_UI012”、“AB_UI013”、“AB_UI06”、“AB_UI016”、“AB_UI07”、“AB_UI018”、“AB_UI019”、“AB_UI015”、“AB_UI020”、“AB_UI02”
],
分拣机=MySort('*!@.()^&%-=+01234567989abcdefghijklmnopqrstuvwxyz');
控制台.日志(项目.分拣(分拣机));
}
它给出了以下回应。
[“AB_UI01”、“AB_UI010”、“AB_UI011”、“AB_UI012”、“AB_UI013”、“AB_UI014”、“AB_UI015”、“AB_UI016”、“AB_UI017”、“AB_UI018”、“AB_UI019”、“AB_UI02”、“AB_UI020”、“AB_UI03”、“AB_UI04”、“AB_UI05”、“AB_UI06”、“AB_UI07”、“AB_UI08”、“AB_UI09”]
未排序数组:
[“AB_UI08”、“AB_UI03”、“AB_UI07”、“AB_UI04”、“AB_UI05”、“AB_UI014”、“AB_UI01”、“AB_UI09”、“AB_UI010”、“AB_UI011”、“AB_UI012”、“AB_UI013”、“AB_UI06”、“AB_UI016”、“AB_UI017”、“AB_UI018”、“AB_UI019”、“AB_UI015”、“AB_UI020”、“AB_UI02”]
预期产出:
[“AB_UI01”、“AB_UI02”、“AB_UI03”、“AB_UI04”、“AB_UI05”、“AB_UI06”、“AB_UI07”、“AB_UI08”、“AB_UI09”、“AB_UI010”、“AB_UI011”、“AB_UI012”、“AB_UI013”、“AB_UI014”、“AB_UI015”、“AB_UI016”、“AB_UI17”、“AB_UI018”、“AB_UI019”、“AB_UI020”]
感谢您的建议。如果您喜欢按组排序,如字母和/或数字,您可以使用以下选项进行排序
var数组=[“AB_UI08”、“AB_UI03”、“AB_UI07”、“AB_UI04”、“AB_UI05”、“AB_UI014”、“AB_UI01”、“AB_UI09”、“AB_UI010”、“AB_UI011”、“AB_UI012”、“AB_UI013”、“AB_UI06”、“AB_UI016”、“AB_UI07”、“AB_UI018”、“AB_UI019”、“AB_UI015”、“AB_UI020”、“AB_UI02”];
log(array.sort((a,b)=>a.localeCompare(b,未定义,{numeric:true,sensitivity:'base'}))代码>
.as console wrapper{max height:100%!important;top:0;}
考虑到您的单词(例如AB_UI08
)看起来像{letters}{digits}
,其中字母
不是数字
和数字
的形式[0-9]+/code>
也就是说:letters==AB_UI
和d==08
假设您想先按字母排序,然后按数字排序
您可以将单词映射为字母和数字,然后为了比较两个单词,请在字母部分相等时比较它们的数字
函数toObj(w){
常量字母=w.match(/^[^\d]+/)[0]
返回{w,字母,d:parseInt(w.replace(字母,'))
}
施工项目=[“AB_UI08”、“AB_UI03”、“AB_UI07”、“AB_UI04”、“AB_UI05”、“AB_UI014”、“AB_UI01”、“AB_UI09”、“AB_UI010”、“AB_UI011”、“AB_UI012”、“AB_UI013”、“AB_UI06”、“AB_UI016”、“AB_UI07”、“AB_UI018”、“AB_UI019”、“AB_UI015”、“AB_UI020”、“AB_UI02”]
const sorted=items.map(toObj).sort((a,b)=>{
const res=a.letters.localeCompare(b.letters)
返回res!==0?res:a.d-b.d
}).map(x=>x.w)
console.log('sorted',sorted)
您想要哪个顺序?顺便说一句,我看到了不同的项目。我想以升序查看。编辑并更新了与输入内联的输出。@NinaScholz使用未排序和预期的数组编辑了查询。太好了!感谢您提供的解决方案,它正在按照我的要求工作。@MuralikrishnaKonduru我感谢您接受我的回答,但是Nina Scholz建议的localeCompare可能更好(更短,并且使用现有api)。干杯!感谢您的时间@NinaScholz,它帮助我解决了我的问题。@Nina Scholz我对数值属性感兴趣,但我找不到任何指定数值属性预期行为的规范(我的意思是您提供的链接上有浏览器兼容性表,但没有“规范”)。如果你有任何w3c文档或其他东西的链接,那将非常感谢!都在上面的链接上。不,只是MDN。好的,睁开眼睛,本地比较不是ecma-262而是