AllInWorld99 provides a reference manual covering many aspects of web programming, including technologies such as HTML, XHTML, CSS, XML, JavaScript, PHP, ASP, SQL,FLASH, jQuery, java, for loop, switch case, if, if else, for...of, for...in, for...each,while loop, blogger tips, blogger meta tag generator, blogger tricks, blogger pagination, client side script, html code editor, javascript editor with instant output, css editor, online html editor, materialize css tutorial, materialize css dropdown list,break, continue statement, label,array, json, get day and month dropdown list using c# code, CSS button,protect cd or pendrive from virus, cordova, android example, html and css to make android app, html code play,telerik show hide column, Transparent image convertor, copy to clipboard using javascript without using any swf file, simple animation using css, SQL etc. AllInWorld99 presents thousands of code examples (accompanied with source code) which can be copied/downloaded independantly. By using the online editor provided,readers can edit the examples and execute the code experimentally.


     In javascript will when we use the sort() method to sort the array, I will not give the exact result for numbers, it will sort the words only.

eg:)
    var ar=[45,9,200];
     Although 9 is numerically smaller than 45 or 200, lexicographically, it is larger, so 9 appears at the very right of the sorted array. Remember, by default array.sort() sorts its elements in lexicographical order.

Answer is
    ar.sort()   => [9,45,200]

 So we can use the following method to solve this problem.


sort() for numbers

    ar_num.sort(function(a, b)
    {
      return a - b;
    });


reverse() for numbers

    ar_num.sort(function(a, b)
    {
      return b - a;
    });

Example Program:- (Editor)


Editor is Loading...

Advertisement





Tag:
sort() method not working in numbers, reverse() not working in number in javascript, allinworld99 to solve number sort() and reverse() method.

0 comments:

Post a Comment

Total Pageviews