LoginSignup
0
0

ずっと使ってる人だと当然だろうけど、たまに使うと忘れててやらかすもの。

> [Number(3),Number(15),Number(10),Number(1)].sort()
> (4) [1, 10, 15, 3]0: 11: 102: 153: 3length: 4[[Prototype]]: Array(0)

なんでかというと下記の仕様の通り、文字列に変換されてからソートされるから。
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
ただ数値順にしたいだけでもこうせにゃならん。

~.sort(
    (a, b) => {
        return (a > b ? 1 : -1);
    }
);
0
0
1

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