0
Sorting In Javascript
How to sort string data type in Javascript without using array
3 Réponses
+ 1
In JS, the sort() method default to the lexicographically order... and if you need to customize the ordering, you could implement your own comparison function and pass it as callback argument to the sort() method:
function my_custom_sort(a,b) {
if (a<b) return -1; // (any negative value)
if (a>b) return 1; // (any positive value)
return 0; // (any falsy value, so no return value will work too)
}
array.sort(mycustom_sort);
0
you use .sort() method for the sorting.
for example to sort var x=[3,1,4,2]
you can use x.sort(), this is applied to the string value also which sort in the alphabetic order
0
Thanks for reply..but i want string to be sorted in lexicographically manner