数値文字列の並び替え方法を備忘録として残しておく
方法
localeCompare メソッドの option numeric を使用する
例.
- 数値以外の文字を含むパターン
- Before Sort
- [“v1.0”, “v2.1”, “v1.4”, “v2.0”, “v1.11.3”, “v1.1.4”, “v1.1.1”]
- After Sort
- [“v2.1”, “v2.0”, “v1.11.3”, “v1.4”, “v1.1.4”, “v1.1.1”, “v1.0”]
- Before Sort
- 数値以外の文字を含まないパターン
- Before Sort
- [“1.0”, “2.1”, “1.4”, “2.0”, “1.11.3”, “1.1.4”, “1.1.1”]
- After Sort
- [“2.1”, “2.0”, “1.11.3”, “1.4”, “1.1.4”, “1.1.1”, “1.0”]
- Before Sort