2

Hello I want to sort my array of strings

String[] listaU = new String[3];

using

Arrays.sort(listaU);

It works well when I make this array full, but when I add for example only 2 strings it doesn't work. Is there any way to sort not "finished" array?

3
  • which sorting result will you expect? Commented May 7, 2015 at 15:28
  • Have you considered initializing the array to values that will be sorted to the very end (or begining)? Commented May 7, 2015 at 15:35
  • 2
    With java-8, you could do Arrays.sort(listaU, Comparator.nullsFirst(Comparator.naturalOrder())); or nullsLast. Commented May 7, 2015 at 15:35

3 Answers 3

2

Use the Arrays.sort(Object[] a, int fromIndex, int toIndex) method.

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

6 Comments

What if OP set only first and last element?
Then OP should have written a better question :-).
question does not says elements are correlative, so you must better reading
"Then OP should have written a better question :-)" question is OK, if you need farther informations from OP ask about them in comment.
I don't understand. By default, if you leave you leave nulls in the array, then Arrays.sort(Object[]) fails with NullPointerException. You can use the fromIndex/toIndex to limit the sorting to a subset of the array that contains non-null values.
|
1

Arrays.sort can be used only on array of elements which are Comparable (contain compareTo) method.

Also arrays of objects (like Strings) are filled with nulls and you can't invoke

null.compareTo("foo");

because null doesn't have any type, which also means no methods. It will simply throw NullPointerException when we try to execute such code.

What you need to do is use Arrays.sort(array, comparator) where your comparator will handle cases where it will be comparing nulls.

Lets say that we want to move all nulls at the end of array. Lets also assume case insensitive order of elements. In that case your code could look like

Arrays.sort(arr, new Comparator<String>() {

    @Override
    public int compare(String o1, String o2) {
        if (o1 == null && o2 == null)   return 0;// no swap needed
        if (o1 == null)                 return 1;// null is bigger, swap left with right
        if (o2 == null)                 return -1;// null is smaller
        return String.CASE_INSENSITIVE_ORDER.compare(o1, o2);
    }

});

Since Java 8 above code can simplified to

Arrays.sort(arr, Comparator.nullsLast(String.CASE_INSENSITIVE_ORDER));

Comments

0

Implement your own comparator that handles null as desired, for example:

String[] listaU = new String[3];

// add elements you need

Arrays.sort(listaU, new Comparator<String>() {
    public int compare(String s1, String s2) {
        if (s1 == null && s2 == null) return 0;
        if (s1 == null) return 1;
        if (s2 == null) return -1;
        return s1.compare(s2);
    }
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.