Answers for "comparator with collections.sort"

1

collections.sort custom comparator

ArrayList<Employee> employees = getUnsortedEmployeeList();
             
Comparator<Employee> compareById = (Employee o1, Employee o2) -> o1.getId().compareTo( o2.getId() );
 
Collections.sort(employees, compareById);
 
Collections.sort(employees, compareById.reversed());
Posted by: Guest on June-28-2020
0

sorting collections with comparator java

public class MarvelComparator {

    public static void main(String... comparator) {
        List<String> marvelHeroes = new ArrayList<>();

        marvelHeroes.add("SpiderMan ");
        marvelHeroes.add("Wolverine ");
        marvelHeroes.add("Xavier ");
        marvelHeroes.add("Cyclops ");


        Collections.sort(marvelHeroes, new Comparator<String>() {
            @Override
            public int compare(String hero1, String hero2) {
                return hero1.compareTo(hero2);
            }
        });

        Collections.sort(marvelHeroes, (m1, m2) -> m1.compareTo(m2));

        Collections.sort(marvelHeroes, Comparator.naturalOrder());

        marvelHeroes.forEach(System.out::print);
    }
}
Posted by: Guest on August-10-2021
1

collections.sort custom comparator

ArrayList<Employee> employees = getUnsortedEmployeeList();
             
Comparator<Employee> compareById = (Employee o1, Employee o2) -> o1.getId().compareTo( o2.getId() );
 
Collections.sort(employees, compareById);
 
Collections.sort(employees, compareById.reversed());
Posted by: Guest on June-28-2020
0

sorting collections with comparator java

public class MarvelComparator {

    public static void main(String... comparator) {
        List<String> marvelHeroes = new ArrayList<>();

        marvelHeroes.add("SpiderMan ");
        marvelHeroes.add("Wolverine ");
        marvelHeroes.add("Xavier ");
        marvelHeroes.add("Cyclops ");


        Collections.sort(marvelHeroes, new Comparator<String>() {
            @Override
            public int compare(String hero1, String hero2) {
                return hero1.compareTo(hero2);
            }
        });

        Collections.sort(marvelHeroes, (m1, m2) -> m1.compareTo(m2));

        Collections.sort(marvelHeroes, Comparator.naturalOrder());

        marvelHeroes.forEach(System.out::print);
    }
}
Posted by: Guest on August-10-2021

Code answers related to "comparator with collections.sort"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language