JavaScriptのsortメソッドで文字列を任意の順番で入れ替え | 古川博章 Web制作
JavaScriptのsortメソッドで文字列を任意の順番で入れ替え | 古川博章 Web制作
code:js
const jyunban = '人類', '第二の人類', '光る人', '火星人', '水の人';
const taiyokei = [
{ namae: '水星', jyunin: '水の人' },
{ namae: '金星', jyunin: '光る人' },
{ namae: '地球', jyunin: '人類' },
{ namae: '火星', jyunin: '火星人' },
{ namae: '木星', jyunin: '第二の人類' },
];
console.log(taiyokei.sort(function(x, y) {
return jyunban.indexOf(x.jyunin) - jyunban.indexOf(y.jyunin);
}));