Anurag Tomar
How you will sort an Array using javascript (Number Array, Character Array)?
By Anurag Tomar in JavaScript on Apr 12 2016
  • Bidyasagar Mishra
    Aug, 2019 2


    • 0
  • Anurag Tomar
    Apr, 2016 12

    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 :)

    • 0

Most Popular Job Functions