“JavaScript Sort Array Descending Order” Kode Jawaban

Urutkan untuk membalikkan dari array di JavaScript

const friends = ["Abir", "Ashik", "Alif", "Alfi", "Shafi", "Kafi"];
const friendsSort = friends.sort();
const friendsReverse = friendsSort.reverse();
console.log(friendsReverse);
//Output: [ 'Shafi', 'Kafi', 'Ashik', 'Alif', 'Alfi', 'Abir' ]
Ariful Islam Adil(Code Lover)

Javascript naik dan turun

// ascending and discending for number
const arr1 = [21, 2100, 2, 35000];
const arr2 = [21, 2100, 2, 35000];

let ascN = arr1.sort((f, s) => f - s);
let dscN = arr2.sort((f, s) => s - f);

// ascending and discending for string
const arr3 = ['21', '2100', '2', '35000'];
const arr4 = ['21', '2100', '2', '35000'];

let ascS = arr3.sort((f, s) => f.length - s.length);
let dscS = arr4.sort((f, s) => s.length - f.length);
Restu Wahyu Saputra

JavaScript Sort Array Descending Order

//used arraow functions
const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]
const sortYears = arr =>{
  arr.sort((a,b)=>{return b-a})
  return arr
}
console.log(sortYears(years))
// Should print [ 2018, 2011, 1999, 1982, 1970, 1963, 1951, 1922 ]
EMMANUEL

Jawaban yang mirip dengan “JavaScript Sort Array Descending Order”

Pertanyaan yang mirip dengan “JavaScript Sort Array Descending Order”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya