List iterators guarantee first and foremost that you get the list’s elements in the internal order of the list (aka. insertion order). More specifically it is in the order you’ve inserted the elements or on how you’ve manipulated the list. Sorting can be seen as a manipulation of the data structure, and there are several ways to sort the list.
1. Sort your list with Set Collections
A sorted Set automatically sorts the collection at insertion, meaning that it does the sorting while you add elements into the collection. TreeSet implements the SortedSet interface so duplicate values are not allowed.
If you are sure that you don’t need to worry about duplicate elements then you can use the TreeSet<T>. It implements SortedSet and NavigableSet interfaces and works as you’d probably expect from a list:
TreeSet<String> set = new TreeSet<String>(); set.add("lol"); set.add("cat"); // automatically sorts natural order when adding for (String s : set) { System.out.println(s); }// Prints out "cat" and "lol"
If you don’t want the natural ordering you can use the constructor parameter that takes a Comparator<T>.
2. Sort your list with Collections.sort()
You can sort your list with the java.util.Collections.sort() method. Here is a code sample on how:
List<String> strings = new ArrayList<String>() strings.add("lol2"); strings.add("lol3"); strings.add("cat"); Collections.sort(strings); for (String s : strings) { System.out.println(s); } // Prints out "cat", "lol2", "lol3"
Using comparators
One clear benefit is that you may use Comparator in the sort method. Java also provides some implementations for the Comparator such as the Collator which is useful for locale sensitive sorting strings. Here is one example:
Collator usCollator = Collator.getInstance(Locale.US); usCollator.setStrength(Collator.PRIMARY); // ignores casing Collections.sort(strings, usCollator);
3. Sort your list with ArrayList.sort()
List<String> names = new ArrayList<>(); names.add("Lisa"); names.add("Jennifer"); names.add("Mark"); names.add("David"); System.out.println("Names : " + names); // Sort an ArrayList using its sort() method. You must pass a Comparator to the ArrayList.sort() method. names.sort(new Comparator<String>() { @Override public int compare(String name1, String name2) { return name1.compareTo(name2); } }); //The above `sort()` method call can also be written simply using lambda expression //names.sort((name1, name2) -> name1.compareTo(name2)); // Following is an even more concise solution //names.sort(Comparator.naturalOrder()); System.out.println("Sorted Names : " + names); } }# Output Names : [Lisa, Jennifer, Mark, David] Sorted Names : [David, Jennifer, Lisa, Mark]
4. Sort an ArrayList of Objects using custom Comparator
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "{" + "name='" + name + '\'' + ", age=" + age + '}'; } } public class ArrayListObjectSortExample { public static void main(String[] args) { List<Person> people = new ArrayList<>(); people.add(new Person("Sachin", 47)); people.add(new Person("Chris", 34)); people.add(new Person("Rajeev", 25)); people.add(new Person("David", 31)); System.out.println("Person List : " + people); // Sort People by their Age people.sort((person1, person2) -> { return person1.getAge() - person2.getAge(); }); // A more concise way of writing the above sorting function //people.sort(Comparator.comparingInt(Person::getAge)); System.out.println("Sorted Person List by Age : " + people); // You can also sort using Collections.sort() method by passing the custom Comparator Collections.sort(people, Comparator.comparing(Person::getName)); System.out.println("Sorted Person List by Name : " + people); } }# Output Person List : [{name='Sachin', age=47}, {name='Chris', age=34}, {name='Rajeev', age=25}, {name='David', age=31}] Sorted Person List by Age : [{name='Rajeev', age=25}, {name='David', age=31}, {name='Chris', age=34}, {name='Sachin', age=47}] Sorted Person List by Name : [{name='Chris', age=34}, {name='David', age=31}, {name='Rajeev', age=25}, {name='Sachin', age=47}]
source:
https://stackoverflow.com/questions/8725387/why-is-there-no-sortedlist-in-java