LoginSignup
2
1

More than 1 year has passed since last update.

JavaScriptで配列を降順に並べる方法です。

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>配列の並び替え</title>
</head>
<body>
  <script>
    const array1 = ["kosaka","kageyama","kato"];
    // 配列を辞書順に並べる
    array1.sort();
    // 配列の要素の1番最後から順に並び替える
    array1.reverse();
    const num = array1.length;
    for(let i=0;i<num;i++){
      document.write(`<p>${array1[i]}</p>`);
    }  
  </script>
</body>
</html>

JavaScriptでは
配列にsort関数を使って昇順に並べた後、Reverse関数を使って要素の1番最後から1番最初に順番を入れ替えます。

const array1 = ["kosaka","kageyama","kato"];
    // 配列を辞書順に並べる
    array1.sort();
    // 配列の要素の1番最後から順に並び替える
    array1.reverse();
    const num = array1.length;
    for(let i=0;i<num;i++){
      document.write(`<p>${array1[i]}</p>`);
    }  
2
1
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
1