LoginSignup
0
0

More than 1 year has passed since last update.

JavaScriptのsortとreverseで勘違いしていたこと

Last updated at Posted at 2021-05-03

 ちょっと用事があって、JavaScriptをしたところ、しばらく悩んでしまいました。
 それは、数値の降順ソートです。
 たとえば、

var arr = [1, 2, 5, 4, 3];
arr.reverse();

で、降順ソート結果

5, 4, 3, 2, 1

を期待というか、勝手に確信してしまっていたのですが、

3, 4, 5, 2, 1

になることに気が付かず、時間を食ってしまいました。
 数値の降順ソートをするには、

var arr = [1, 2, 5, 4, 3];
arr.sort();
arr.reverse();

なんですね。

※当たり前といえば、当たり前過ぎですね。
※node.jsで試しました。

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