Skip to content

Instantly share code, notes, and snippets.

@rmg007
Created October 16, 2022 14:10
Show Gist options
  • Select an option

  • Save rmg007/cf0b2fdf34e9e0a8fb6635f3ff641955 to your computer and use it in GitHub Desktop.

Select an option

Save rmg007/cf0b2fdf34e9e0a8fb6635f3ff641955 to your computer and use it in GitHub Desktop.
Sort List Using Comparator Nulls First
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