Minggu, 27 Januari 2019

How To Classify In Addition To Opposite Inward Array Numbers Using Javascript

     In javascript volition when nosotros utilisation the sort() method to form the array, I volition non rank the exact final result for numbers, it volition form the words only.

eg:)
    var ar=[45,9,200];
     Although nine is numerically smaller than 45 or 200, lexicographically, it is larger, together with then nine appears at the really correct of the sorted array. Remember, past times default array.sort() sorts its elements inwards lexicographical order.

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

 So nosotros tin cease utilisation the next method to solve this problem.


sort() for numbers
     ar_num.sort(function(a, b)
    {
      provide a - b;
    });


reverse() for numbers
     ar_num.sort(function(a, b)
    {
      provide b - a;
    });

Example Program:- (Editor)


Editor is Loading...

Advertisement





Tag:
sort() method non working inwards numbers, reverse() non working inwards disclose inwards javascript, allinworld99 to solve disclose sort() together with reverse() method.

Tidak ada komentar:

Posting Komentar