“JavaScript orderby” Kode Jawaban

JS Array Sort

numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
Worried Wildebeest

JavaScript orderby

var items = [
  { name: 'Edward', value: 21 },
  { name: 'Sharpe', value: 37 },
  { name: 'And', value: 45 },
  { name: 'The', value: -12 },
  { name: 'Magnetic', value: 13 },
  { name: 'Zeros', value: 37 }
];

// sort by value
items.sort(function (a, b) {
  return a.value - b.value;
});

// sort by name
items.sort(function(a, b) {
  var nameA = a.name.toUpperCase(); // ignore upper and lowercase
  var nameB = b.name.toUpperCase(); // ignore upper and lowercase
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  // names must be equal
  return 0;
});
Clever Crocodile

Urutkan JS

numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
Colorful Cockroach

jenis javascript

homes.sort(function(a, b) {
    return parseFloat(a.price) - parseFloat(b.price);
});
Fancy Fox

Metode Urutkan JS

let numbers = [0, 1 , 2, 3, 10, 20, 30 ];
numbers.sort( function( a , b){
    if(a > b) return 1;
    if(a < b) return -1;
    return 0;
});

console.log(numbers);
Code language: JavaScript (javascript)
SuB0D00

jenis javascript

homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
Fancy Fox

Jawaban yang mirip dengan “JavaScript orderby”

Pertanyaan yang mirip dengan “JavaScript orderby”

Lebih banyak jawaban terkait untuk “JavaScript orderby” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya