Created
March 15, 2016 07:59
-
-
Save toandv/95b0576de09d6e6942ce to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.List; | |
public class Comparings { | |
public static void main(String[] args) { | |
List<User> list = Arrays.asList(new User("a"), new User("b"), new User(""), new User(null)); | |
// | |
// list.sort((l, r) -> Comparator.nullsFirst(Comparator.nullsFirst(Comparator.naturalOrder())).compare(l.name, | |
// r.name)); | |
System.out.println(list); | |
List<User> list1 = Arrays.asList(new User("a"), new User("b"), new User(""), new User(null)); | |
list1.sort(Comparator.comparing(user -> user.name, Comparator.nullsFirst(Comparator.naturalOrder()))); | |
System.out.println(list1); | |
} | |
static class User { | |
String name; | |
public User(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return "User [name=" + name + "]"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment