2

I have an array similar to that:

[null, null, 3, 4, null]

I want to change it to array similar to that:

[3, 4, null, null, null]

I know how to do it manually, but it looks ugly. Is there any built-in(with minimum custom code) way to do that? (maybe something with System.arraycopy)

I finished with something like this(thanks @Braj for idea):

@Test
public void test() {
  Integer[] k = new Integer[]{null, null, 3, 2, 1};
  k[1] = null;
  Arrays.sort(k, new Comparator<Object>() {
    public int compare(Object o1, Object o2) {
      System.out.println(o1);
      if (o1 == null) return 1;
      else if(o2 == null) return -1;
      else return 0;
    }
  });
  assertArrayEquals(
    new Integer[]{3, 2, 1, null, null},
    k
  );
}

If somebody interested there is a bigger problem which tried to solve with this code

6
  • 2
    Please include the code you have tried. Commented Jul 5, 2014 at 11:40
  • Do you care about the ordering of non-null elements? Should they be sorted in any specific way? Should they retain their initial order? Commented Jul 5, 2014 at 11:41
  • Yes, they should retain their initial order Commented Jul 5, 2014 at 11:43
  • 1
    1) It looks ugly --> put that ugly code in a method, and you no longer have to see that ugly code (abstraction). As for using the API, there is a java.util.Arrays. If it is not there, you will waste more time searching for a solution than solving it yourself Commented Jul 5, 2014 at 11:43
  • You can take a look at Guava and its nullsLast ordering. I'm not sure you can use it without sorting the collection in any way at the same time so it may not be that concise a solution. Commented Jul 5, 2014 at 11:48

2 Answers 2

6

Use Arrays.sort(T[],Comparator) and pass Comparator to sort based on your own logic.

Read more here and find sample code as well


Here is the sample code that sorts the array as well while pushing null values in the end.

Note: (change it as per your need, not fully tested)

    final Integer[] data = new Integer[] { null, null, 3, 4, null };
    Arrays.sort(data, new Comparator<Integer>() {
        public int compare(Integer o1, Integer o2) {
            if(o1 != null && o2!=null){
                return o1.compareTo(o2);
            }else if(o2 != null){
                return o2;
            }else{                  
                return Integer.MIN_VALUE; // null values in the end
            }
        }
    });

    System.out.println(Arrays.toString(data));

output:

   [3, 4, null, null, null]

EDIT

If you are interested only in moving the null values in the end then find sample code in the below post that is answered by @Dmitry Ginzburg

Sign up to request clarification or add additional context in comments.

12 Comments

on [null, null, 4, 3, null] this code would return [3, 4, null, null, null], which is incorrect.
@DmitryGinzburg what is expected?
@Braj [4, 3, null, null, null]
@Braj I adopted your solution a little bit. Can you check it(now it's a part of question)?
@kharandziuk use Comparator<Integer> instead of Comparator<Object>
|
2

The sort algo from Java standard library is stable, so, if we're making the elements, which are not null the same (in the context of ordering), it would keep the order:

Arrays.sort(data, new Comparator<Integer>() {
    public int compare (Integer x1, Integer x2) {
        if (x1 == null && x2 != null)
            return 1;
        else if (x1 != null && x2 == null)
            return -1;
        else
            return 0;
    }
});

4 Comments

Incorrect result. I tested your code for input [null, null, 3, 4, 2, null] and output [3, 4, 2, null, null, null]
Your code is correct, but Braj provided an idea earlier. Thanks!
@DmitryGinzburg Sorry I have taken it in wrong way. I was thinking from sorting perspective as well. As per sorting logic it should be []2, 3, 4, null, null, null] but I was wrong. I think OP just want to move the items and not interested in sorting.
@Braj, yep, it's really what OP requesting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.