Created
October 16, 2022 14:10
-
-
Save rmg007/cf0b2fdf34e9e0a8fb6635f3ff641955 to your computer and use it in GitHub Desktop.
Sort List Using Comparator Nulls First
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
| import java.util.ArrayList; | |
| import java.util.Comparator; | |
| import java.util.List; | |
| public class SortListUsingComparatorNullsFirst { | |
| public static void main(String[] args) { | |
| List<Integer> list = new ArrayList<>(){{ | |
| add(6); | |
| add(null); | |
| add(8); | |
| add(null); | |
| add(4); | |
| }}; | |
| //output: [null, null, 4, 6, 8] | |
| list.sort(Comparator.nullsFirst(Comparator.naturalOrder())); | |
| System.out.println(list); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment