如何在JavaScript中按字母顺序排列以下项目列表?按字母顺序排列项目列表
Item 1, Item 12, Item 3, Item 4, Item 5
,其结果应该是:
Item 1, Item 3, Item 4, Item 5, Item 12
如何在JavaScript中按字母顺序排列以下项目列表?按字母顺序排列项目列表
Item 1, Item 12, Item 3, Item 4, Item 5
,其结果应该是:
Item 1, Item 3, Item 4, Item 5, Item 12
什么你要找的是自然排序,这可以帮助你:
阅读这些链接的内容,您将能够首先按字母排序,然后数字CAL。
最容易和清洁的方法是这样的:
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
你想要的字母排列,或'项目3'之前'项目12'? –
艾哈好问题@RaphaëlAlthaus。 – Pavan
我有一种感觉,“Item 12”是一个拼写错误,因为其他所有内容都增加了1. – knrz