Sorting In Javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Sorting In Javascript

How to sort string data type in Javascript without using array

16th Apr 2020, 9:29 AM
Bhavleen Singh Manaktala
Bhavleen Singh Manaktala - avatar
3 Answers
+ 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);
16th Apr 2020, 2:48 PM
visph
visph - avatar
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
16th Apr 2020, 2:13 PM
Angad Lamichhane
Angad Lamichhane - avatar
0
Thanks for reply..but i want string to be sorted in lexicographically manner
16th Apr 2020, 2:19 PM
Bhavleen Singh Manaktala
Bhavleen Singh Manaktala - avatar