Sorting arrays using for loop in Javascript

前端 未结 6 1035
灰色年华
灰色年华 2021-01-15 16:36

Is it possible to sort arrays in ascending/descending order using the for loop JavaScript?

I\'ve been learning JS going through a few practice quest

6条回答
  •  攒了一身酷
    2021-01-15 17:01

    If you want to get the max/min number in an array, why not use Math.max/Math.min?

    If you want to sort the array, you can use sort method:

    var sorted = [3, 1, 6, 2].sort(); // sort ascending  
    var sorted = [3, 1, 6, 2].sort(function(a, b){
        return b - a;
    }); // sort descending 
    

提交回复
热议问题