JSの配列のソート
- <!DOCTYPE html>
- <html lang="ja">
- <head>
- <meta charset="UTF-8">
- <title>JSの配列のソート</title>
- </head>
- <body>
- <h1>JSの配列のソート</h1>
- <script type="text/javascript">
- // 数字でソート
- ary = new Array("3", "2", "1", "10", "6", "5");
- ary.sort();
- document.write("<h2>数字を数字(文字として)でソート。xxx.sort()</h2>");
- for (i=0; i<ary.length; i++)
- {
- document.write(ary[i] + " - ");
- }
-
- // 数値でソート
- ary = new Array("3", "2", "1", "10", "6", "5");
- ary.sort(function(a,b){return a-b;});
- document.write("<h2>数字を数値でソート。xxx.sort(function(a,b){return a-b;})</h2>");
- for (i=0; i<ary.length; i++)
- {
- document.write(ary[i] + " - ");
- }
-
- // 数値でソート:逆順
- ary = new Array("3", "2", "1", "10", "6", "5");
- ary.sort(function(a,b){return b-a;});
- document.write("<h2>数字を数値でソート:逆順。xxx.sort(function(a,b){return b-a;})</h2>");
- for (i=0; i<ary.length; i++)
- {
- document.write(ary[i] + " - ");
- }
-
- // 文字のソート
- ary = new Array("b", "d", "a", "c", "z", "x");
- ary.sort();
- document.write("<h2>文字のソート。xxx.sort()</h2>");
- for (i=0; i<ary.length; i++)
- {
- document.write(ary[i] + " - ");
- }
-
- // 文字のソート:逆順
- ary = new Array("b", "d", "a", "c", "z", "x");
- ary.reverse(ary.sort());
- document.write("<h2>文字のソート:逆順。ary.reverse(xxx.sort());</h2>");
- for (i=0; i<ary.length; i++)
- {
- document.write(ary[i] + " - ");
- }
-
- // 連想配列(指定したキーの値が数値)のソート
- ary = new Array(
- {id: 1, item: "ねぎ間", price: 168},
- {id: 2, item: "ハツ", price: 136},
- {id: 3, item: "レバー", price: 136},
- {id: 4, item: "ぼんちり", price: 136},
- {id: 5, item: "はらみ", price: 136},
- {id: 6, item: "うずら玉子", price: 136},
- {id: 7, item: "なんこつ", price: 136},
- {id: 8, item: "手羽先", price: 168},
- {id: 9, item: "せせり", price: 136},
- {id: 10, item: "白レバー", price: 168},
- {id: 11, item: "丸ごとシマウマ", price: 290}
- );
- ary.sort(function(a,b){return a.price-b.price;});
- document.write("<h2>連想配列のソート(指定したキーの値が数値)。xxx.sort(function(a,b){return a.price-b.price;})</h2>");
- for (i=0; i<ary.length; i++)
- {
- document.write("( ");
- document.write(ary[i]["id"] + " - ");
- document.write(ary[i]["item"] + " - ");
- document.write(ary[i]["price"]);
- document.write(" )<br>");
- }
-
- // 連想配列(指定したキーの値が数値)のソート:逆順
- ary = new Array(
- {id: 1, item: "ねぎ間", price: 168},
- {id: 2, item: "ハツ", price: 136},
- {id: 3, item: "レバー", price: 136},
- {id: 4, item: "ぼんちり", price: 136},
- {id: 5, item: "はらみ", price: 136},
- {id: 6, item: "うずら玉子", price: 136},
- {id: 7, item: "なんこつ", price: 136},
- {id: 8, item: "手羽先", price: 168},
- {id: 9, item: "せせり", price: 136},
- {id: 10, item: "白レバー", price: 168},
- {id: 11, item: "丸ごとシマウマ", price: 290}
- );
- ary.sort(function(a,b){return b.price-a.price;});
- document.write("<h2>連想配列のソート(指定したキーの値が数値)。xxx.sort(function(a,b){return b.price-a.price;})</h2>");
- for (i=0; i<ary.length; i++)
- {
- document.write("( ");
- document.write(ary[i]["id"] + " - ");
- document.write(ary[i]["item"] + " - ");
- document.write(ary[i]["price"]);
- document.write(" )<br>");
- }
-
- // 連想配列(指定したキーの値が文字)のソート
- ary = new Array(
- {id: 1, item: "ねぎ間", price: 168},
- {id: 2, item: "ハツ", price: 136},
- {id: 3, item: "レバー", price: 136},
- {id: 4, item: "ぼんちり", price: 136},
- {id: 5, item: "はらみ", price: 136},
- {id: 6, item: "うずら玉子", price: 136},
- {id: 7, item: "なんこつ", price: 136},
- {id: 8, item: "手羽先", price: 168},
- {id: 9, item: "せせり", price: 136},
- {id: 10, item: "白レバー", price: 168},
- {id: 11, item: "丸ごとシマウマ", price: 290}
- );
- ary.sort(
- function(a,b)
- {
- var aChar = a.item;
- var bChar = b.item;
- if (aChar < bChar)
- {
- return -1;
- }
- else if (bChar < aChar)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- );
- document.write("<h2>連想配列のソート(指定したキーの値が文字)。xxx.sort(function(a,b){~~~})</h2>");
- for (i=0; i<ary.length; i++)
- {
- document.write("( ");
- document.write(ary[i]["id"] + " - ");
- document.write(ary[i]["item"] + " - ");
- document.write(ary[i]["price"]);
- document.write(" )<br>");
- }
-
- // 連想配列(指定したキーの値が文字)のソート:逆順
- ary = new Array(
- {id: 1, item: "ねぎ間", price: 168},
- {id: 2, item: "ハツ", price: 136},
- {id: 3, item: "レバー", price: 136},
- {id: 4, item: "ぼんちり", price: 136},
- {id: 5, item: "はらみ", price: 136},
- {id: 6, item: "うずら玉子", price: 136},
- {id: 7, item: "なんこつ", price: 136},
- {id: 8, item: "手羽先", price: 168},
- {id: 9, item: "せせり", price: 136},
- {id: 10, item: "白レバー", price: 168},
- {id: 11, item: "丸ごとシマウマ", price: 290}
- );
- ary.sort(
- function(a,b)
- {
- var aChar = a.item;
- var bChar = b.item;
- if (bChar < aChar)
- {
- return -1;
- }
- else if (aChar < bChar)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- );
- document.write("<h2>連想配列のソート(指定したキーの値が文字):逆順。xxx.sort(function(a,b){~~~})</h2>");
- for (i=0; i<ary.length; i++)
- {
- document.write("( ");
- document.write(ary[i]["id"] + " - ");
- document.write(ary[i]["item"] + " - ");
- document.write(ary[i]["price"]);
- document.write(" )<br>");
- }
- </script>
- <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
- <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
- <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
- </div>
- <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
- Copyright © 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
- </div>
- </body>
- </html>