2014-01-14 30 views
1

如何在JavaScript中按字母顺序排列以下项目列表?按字母顺序排列项目列表

Item 1, Item 12, Item 3, Item 4, Item 5 

,其结果应该是:

Item 1, Item 3, Item 4, Item 5, Item 12 
+5

你想要的字母排列,或'项目3'之前'项目12'? –

+0

艾哈好问题@RaphaëlAlthaus。 – Pavan

+0

我有一种感觉,“Item 12”是一个拼写错误,因为其他所有内容都增加了1. – knrz

回答

0

最容易和清洁的方法是这样的:

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 
相关问题