Skip to content

Instantly share code, notes, and snippets.

@canattofilipe
Last active November 27, 2019 13:18
Show Gist options
  • Save canattofilipe/0d77e8e07c9c704e29e2036a55aec23c to your computer and use it in GitHub Desktop.
Save canattofilipe/0d77e8e07c9c704e29e2036a55aec23c to your computer and use it in GitHub Desktop.
Sorting an ArrayList of objects with Java 8
// 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