LoginSignup
2
2

More than 1 year has passed since last update.

JavaScriptでの配列のソート(まとめ)

Posted at

配列の要素の並び替えのまとめです

昇順

const array1 = [200,300,1000,500,100];

array1.sort((a,b) => a > b ? 1:-1);
array1.forEach((data) => {
      document.write(`<p>${data}</p>`);
})

降順

const array1 = [200,300,1000,500,100];
   
array1.sort((a,b) => a < b ? 1:-1);
array1.forEach((data) => {
      document.write(`<p>${data}</p>`);
})
2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2