admin管理员组文章数量:1353261
I have a bination of letters and numbers. For example: 2E12, 1Z10, 3D13, 3D03, FB14, X002, etc.
I've tried a handful of methods to sort these strings, but nothing seems to work. parseInt works in clumps but the whole array is never sorted (it is a json array) and different results appear if the sort is run a second time.
I've also tried using regex to replace all of the letters with numbers, but this creates a logic error. Each time a large letter in the middle of the string is replaced it increases the number by a factor of 10 or 20. For example, 1Z10 would create 12610 even though it starts with a 1 and should sort towards the top.
Does anyone know of how to sort these strings? It doesn't matter if the letter is first or the number is first, as long as I can get away from the random smatterings.
Thanks in advance!
I have a bination of letters and numbers. For example: 2E12, 1Z10, 3D13, 3D03, FB14, X002, etc.
I've tried a handful of methods to sort these strings, but nothing seems to work. parseInt works in clumps but the whole array is never sorted (it is a json array) and different results appear if the sort is run a second time.
I've also tried using regex to replace all of the letters with numbers, but this creates a logic error. Each time a large letter in the middle of the string is replaced it increases the number by a factor of 10 or 20. For example, 1Z10 would create 12610 even though it starts with a 1 and should sort towards the top.
Does anyone know of how to sort these strings? It doesn't matter if the letter is first or the number is first, as long as I can get away from the random smatterings.
Thanks in advance!
Share Improve this question asked May 16, 2011 at 15:34 SortingHatSortingHat 7573 gold badges16 silver badges30 bronze badges 1- 2 Can you give an example sorted order that you are trying to get? A simple string sort should do what it seems you're asking, e.g. javascriptkit./javatutors/arraysort.shtml – Anthony -GISCOE- Commented May 16, 2011 at 15:40
1 Answer
Reset to default 11if you want digit sequences to sort as if they were numbers, before alphas and so that 100 sorts after 2, you need what is called a natural sort-
This is one example, Google will find more.
// case insensitive, digits to number interpolation
function natSort(as, bs){
var a, b, a1, b1, i= 0, L, rx= /(\d+)|(\D+)/g, rd= /\d/;
if(isFinite(as) && isFinite(bs)) return as - bs;
a= String(as).toLowerCase();
b= String(bs).toLowerCase();
if(a=== b) return 0;
if(!(rd.test(a) && rd.test(b))) return a> b? 1: -1;
a= a.match(rx);
b= b.match(rx);
L= a.length> b.length? b.length: a.length;
while(i < L){
a1= a[i];
b1= b[i++];
if(a1!== b1){
if(isFinite(a1) && isFinite(b1)){
if(a1.charAt(0)=== "0") a1= "." + a1;
if(b1.charAt(0)=== "0") b1= "." + b1;
return a1 - b1;
}
else return a1> b1? 1: -1;
}
}
return a.length - b.length;
}
var s= '2E12, 1Z10, 1z2, 3D13, 3D03, FB14, X002'.split(', ');
s.sort(natSort)
/* returned value: (Array)
1z2,1Z10,2E12,3D03,3D13,FB14,X002
*/
本文标签: arraysJavascriptSort Letter Number CombinationStack Overflow
版权声明:本文标题:arrays - Javascript - Sort Letter Number Combination - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743864949a2552411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论