String sort null values

I have a string arraylist with some null values ​​and some strings. I don't want to sort the arraylist, but I have to sort the arraylist so that the null values ​​are the last. Suppose that arraylist {1,2,null,6,5,null, 3} , I should get the values ​​null {1,2,6,5,3,null,null} .

I currently have a solution: Right now, I am building a new arraylist, and if the value is null , I will not drag it to the new list, otherwise I will add it to the new arraylist.

Any other better solution?

Thanks for the help.

+5
source share
8 answers

If you use Java 8, you can easily build the necessary comparator:

 Arrays.sort(stringArray, Comparator.nullsLast(Comparator.naturalOrder())); 

But if you are not using java 8, you may have a comparator like below

 public class StringNullComparator implements Comparator<String> { public int compare(String stringOne, String stringTwo) { if (stringOne != null && stringTwo != null) return stringOne.compareTo(stringTwo); return (stringOne == stringTwo)?0:(stringOne==null? 1 : -1); } } 

And you can use below

 Arrays.sort(stringArray, new StringNullComparator()); 
+8
source

Custom mapper for sorting:

 public class StringComparator implements Comparator<String> { public int compare(String s1, String s2) { if (s1 != null && s2 != null) return s1.compareTo(s2); return (s1 == null) ? 1 : -1; } } 

then

 Collectios.sort(list, new StringComparator()); 
+5
source

If you want to avoid explicit iteration over the entire list, you can use ArrayList.indexOf () to find the null values ​​and then remove () them. If you want to keep the values ​​in a list, you can simply add a null value to the end of the list. However, I would suggest that this approach is not very good in terms of performance if this is a concern.

+1
source

You can use NullComparator from apache .

 Collections.sort(list, new NullComparator()); 
+1
source

how to build a new arraylist, and if this is a real value, add it to the new list, and if it is a zero increment, then a counter. In the latter case, add a null number equal to the counter value.

0
source

If you want to sort zero to the end and keep order for non-zero elements, this Comparator will do this:

 class CompareStrings implements Comparator<String> { @Override public int compare(String o1, String o2) { if (o1 == null && o2 != null) return 1; if (o2 == null && o1 != null) return -1; return 0; } } 

If both String are null or nonzero, they will compare them. If only one of them is null, it will be compared with less than nonzero.

0
source

What about:

 class MyInteger implements Comparator<Integer> { public int compare(Integer arg0, Integer arg1) { if(arg1 == null) { return -1; } return 0; } } 

And we can use it like:

 List<Integer> al = new ArrayList<Integer>(); al.add(1); al.add(2); al.add(null); al.add(6); al.add(5); al.add(null); al.add(3); Collections.sort(al, new MyInteger()); 
0
source

All other solutions include sorting. As you mentioned, you do not need sorting. If time complexity is a problem, you can use the following linear time solution (in place):

  public static <T> void nullsToEndInPlace(List<T> l) { int i = 0; int j = l.size() - 1; while (i < j) { T left = l.get(i); T right = l.get(j); if (left != null) { i++; } else if (right == null) { j--; } else { l.set(i, right); l.set(j, null); i++; j--; } } } 
0
source

Source: https://habr.com/ru/post/1237157/


All Articles