Javascript 按字母顺序排列项目列表
在javascript中,我们如何按字母顺序排列下面的项目列表Javascript 按字母顺序排列项目列表,javascript,list,sorting,alphabetical,Javascript,List,Sorting,Alphabetical,在javascript中,我们如何按字母顺序排列下面的项目列表 Item 1, Item 12, Item 3, Item 4, Item 5 结果应该是: Item 1, Item 3, Item 4, Item 5, Item 12 array.sort()是您要查找的内容 您需要的是自然排序,这可以帮助您: 阅读这些链接中的内容,您可以先按字母顺序,然后按数字顺序排列项目。最简单、最简洁的方法是: var your_array = [item 1, item 2, item
Item 1, Item 12, Item 3, Item 4, Item 5
结果应该是:
Item 1, Item 3, Item 4, Item 5, Item 12
array.sort()
是您要查找的内容
您需要的是自然排序,这可以帮助您:
阅读这些链接中的内容,您可以先按字母顺序,然后按数字顺序排列项目。最简单、最简洁的方法是:
var your_array = [item 1, item 2, item 3, ...item i];
var sorted_array = your_array.sort(); //this sorts alphabetically but not numerically
var sortedNumerically = your_array.sort(function(a,b){ return a-b;}) //this sorts numerically in ascending order
你想在
项目12
之前按字母顺序排序,或者项目3
?这是一个好问题@RaphaëlAlthaus。我感觉项目12
是一个打字错误,因为其他所有内容都是按1递增的。@knrz但是为什么要对已经排序的内容进行排序呢?我想他只是抽象了他的字符串。不管怎样,让我们等到OP澄清。
var your_array = [item 1, item 2, item 3, ...item i];
var sorted_array = your_array.sort(); //this sorts alphabetically but not numerically
var sortedNumerically = your_array.sort(function(a,b){ return a-b;}) //this sorts numerically in ascending order