1
Reply

How you will sort an Array using javascript (Number Array, Character Array)?

Anurag Tomar

Anurag Tomar

Apr 12, 2016
230
0

    See this looks like a very simple question but sometime it also puzzle you. In JavaScript Ex 1) : var tempArr = [1,12,8,13,9];tempArr.sort(); // Result [1,12,13,8,9]This is not the correct sorting so what is the right method. See belowEx 2): var tempArr = [1,12,8,13,9];tempArr.sort(function(a,b){return a-b}); // Result [1,8,9,12,13] Which is correctThis is because the default sort order is according to string Unicode code points. Ex 1 will be correct if we will try this for character. See below.Ex 3) : var tempArr = ['anurag','rajiv','ashish','sherest','ravi','sumant']; tempArr.sort(); // Result ["anurag", "ashish", "rajiv", "ravi", "sherest", "sumant"]Happy Coding :)

    Anurag Tomar
    April 12, 2016
    0