Snippets tagged “comparator”
9 snippets use this tag.
- How can I sort Map values by key in Java?Java
To sort the values of a Map by key in Java, you can use the TreeMap class, which is a Map implementation that maintains its entries in ascending key order.
- How to sort a List/ArrayList?Java
To sort a List or ArrayList in Java, you can use the sort method of the Collections class from the java.util package. The sort method takes a List and sorts it in ascending order according to the natural ordering of its elements.
- How to sort a Map<Key> by values in JavaJava
To sort a Map<Key, Value> by values in Java, you can create a custom comparator that compares the values and pass it to the sort() method of the Map.Entry class.
- How to sort an ArrayList in JavaJava
To sort an ArrayList in Java, you can use the Collections.sort method.
- How to use a Java8 lambda to sort a stream in reverse order?Java
To use a Java 8 lambda to sort a stream in reverse order, you can use the sorted() method of the Stream interface and pass a lambda function that compares the elements in reverse order.
- How to use Comparator in Java to sortJava
To use a Comparator in Java to sort a list of objects, you can use the Collections.sort method and pass it a List and a Comparator.
- Sort an array in JavaJava
To sort an array in Java, you can use the Arrays.sort() method of the java.util.Arrays class. This method sorts the elements of the array in ascending order according to their natural ordering.
- Sort ArrayList of custom Objects by propertyJava
To sort an ArrayList of custom objects by a property, you can use the Collections.sort method and pass it a custom Comparator.
- Sorting HashMap by valuesJava
To sort a HashMap by values in Java, you can use the Map.Entry interface and the Comparator interface to create a comparator that compares the values in the map.