0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【JavaScript学習記録】Objectをsortしたい

Posted at

 いつものように、.sort( )で並び替えをし、console.logで確認したところ、中身に変化がない・・・。
 どうやらObjectは、.sort( )では並び替えできないようです。
 どうしたら並び替えることができるのか調べましたので、自分用に記録しておきます。

 ・今回sortしたいデータ

// {'entryNum': time}
finalGameResult = { '1': 870, '2': 855 }; 

 ・これをタイムが早い順で並び替えしたい・・・

 実現する為に、以下のサイトを参考にさせて頂きました:relaxed::tulip:
https://kinocolog.com/javascript_sort/

 同じように当てはめてみる。


let ranking = Object.keys(finalGameResult).map((k) => ({ entryNum: k, time: finalGameResult[k] }));
ranking.sort((a, b) => a.time - b.time);
console.log(ranking);
⇒ [ { entryNum: '2', time: 855 }, { entryNum: '1', time: 870 } ]

 無事に並び替えることができました!

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?