sorting collections with comparator java

Solutions on MaxInterview for sorting collections with comparator java by the best coders in the world

showing results for - "sorting collections with comparator java"
Miriam
17 May 2016
1
2public class MarvelComparator {
3
4    public static void main(String... comparator) {
5        List<String> marvelHeroes = new ArrayList<>();
6
7        marvelHeroes.add("SpiderMan ");
8        marvelHeroes.add("Wolverine ");
9        marvelHeroes.add("Xavier ");
10        marvelHeroes.add("Cyclops ");
11
12
13        Collections.sort(marvelHeroes, new Comparator<String>() {
14            @Override
15            public int compare(String hero1, String hero2) {
16                return hero1.compareTo(hero2);
17            }
18        });
19
20        Collections.sort(marvelHeroes, (m1, m2) -> m1.compareTo(m2));
21
22        Collections.sort(marvelHeroes, Comparator.naturalOrder());
23
24        marvelHeroes.forEach(System.out::print);
25    }
26}
Hannes
06 Aug 2018
1
2Collections.sort(marvel, new Comparator<String>() {
3            @Override
4            public int compare(String hero1, String hero2) {
5                return hero1.compareTo(hero2);
6            }
7        });