Chuyển đến nội dung chính

how to sort and reverse in array numbers using javascript

     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.

Nhận xét

Popular Posts

AutoDWG PDF to DWG Converter Pro 2019 v3.9.1 Crack + Patch + Full Version

AutoDWG PDF to DWG Converter Pro 2019 v3.9.1 Crack + Patch + Full Version AutoDWG PDF to DWG Converter Pro 2019 v3.9.1 Crack + Patch + Full Version, as its name suggests, is a very simple and compact software for converting PDF documents into popular DWG and DXF formats in the CAD world. It also allows you to convert bitmap images into a scalable vector format. This software supports the latest version of AutoCAD 2017 and its output in AutoCAD will open without any incompatibility. This program is provided by AutoDWG, a creative company founded in 2002 and started to specialize in delivering CAD products. The company has been working with a variety of technologies to provide users with a variety of formats such as PDF, DWG , DXF, DWF, SWF, GIF, write, encrypt and share, as well as the ability to work with all of this format and convert between them for work in the CAD world. Due to customer feedback and their requests, the company is constantly working to improve the quality of its pro...

Adobe Photoshop 7.0 Free Download Full Version With Key

Some Screenshots of Adobe Photoshop 7.0 Free Download Adobe Photoshop 7.0 With Keys Full Version! FileSize: 154 MB Download

How to create new project in android studio?

1) Open Android studio, then find the File  option from the menu bar and go to New => New Project. 2) Now one new dialog box is open and ask the project details Application name, Company Domain and project location. Application name       No need to enter unique name compare with playstore, this name is displayed under the application icon. Company Domain       It must be unique compare between the google play store (If you created the name and the same name already in playstore it won't show any error message, but in publishing time it will show a error). Project Location       It will automatically take the previous project path, if you want the same path no need to change it, or you can change. 3) Then press Next button      In this window select the minimum target SDK ,wear, TV, and Android auto. Minimum SDK      Here you can select the minimum required android os version. Wear      ...