How do we arrange the following list of item in alphabetical order in javascript?
Item 1, Item 12, Item 3, Item 4, Item 5
and the result should be:
Item 1, Item 3, Item 4, Item 5, Item 12
How do we arrange the following list of item in alphabetical order in javascript?
Item 1, Item 12, Item 3, Item 4, Item 5
and the result should be:
Item 1, Item 3, Item 4, Item 5, Item 12
array.sort()
is what you're looking for.
[Item1, Item2, Item3, Item4, Item5].sort()
What you're looking for is natural sorting, this could help you:
Reading the content in these links you'll be able to order items first alphabetical, then numerical.
The most easy and clean way is this:
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