2
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.

【Java】Comparatorを使ってListをソートする方法

Posted at

#はじめに
本記事ではJava 8 以降可能となった、Comparatorを使ってListをソートする方法を備忘のためにアウトプットします。

#単項目でソートする

文字列長でソート
List<String> playerList = Arrays.asList("Keisuke Honda", "Shinji Kagawa", "Marcus Tulio Lyuji Murzani Tanaka");
        playerList.sort(Comparator.comparingInt(String::length)
        .collect(Collectors.toList());

#複数条件でソートする

thenComparing()を利用することで、複数条件でソートすることが可能です。

文字列長でソート後、アルファベット順でソート
List<String> playerList = Arrays.asList("Keisuke Honda", "Shinji Kagawa", "Marcus Tulio Lyuji Murzani Tanaka");
        playerList.stream().sorted(
        Comparator.comparingInt(String::length)
        .thenComparing(Comparator.naturalOrder())).collect(Collectors.toList());

#参考
Java Platform SE8 #Comparator

2
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
2
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?