Last active
November 27, 2019 13:18
-
-
Save canattofilipe/0d77e8e07c9c704e29e2036a55aec23c to your computer and use it in GitHub Desktop.
Sorting an ArrayList of objects with Java 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sort arrayList of objects with java 8 | |
List<User> users = new ArrayList<>(); | |
users.add(new User(10L, "Jon")); | |
users.add(new User(120L, "Robb")); | |
users.add(new User(190L, "Sansa")); | |
users.add(new User(100L, "Arya")); | |
users.add(new User(150L, "Bran")); | |
users.sort((u1, u2) -> u1.getName().compareTo(u2.getName())); | |
users.forEach(u -> System.out.println(u.getName())); | |
/* | |
Arya | |
Bran | |
Jon | |
Robb | |
Sansa | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment