2

I need to sort a String[] with integer values in descending order. I am using String[] since the content of the array can either be a string or an integer.

To sort in descending order,

Arrays.sort(rows, Collections.reverseOrder());

where rows contain "14","0","3";

Sorting works fine if string[] contain words.

I read that Collections.reverseOrder() doesn't work with primitive types, but will this fall under that category since am using String[] and not int[]?

If so, do we have a sorting mechanism other than using a for loop?

4
  • Are you saying the array contains both String and Integer instances (shouldn't be possible with a String[]), or that it contains strings of integers, e.g. "14", "0", "3"? Commented Mar 1, 2013 at 11:39
  • yes, the array contains string values of integers, "14","0","3" Commented Mar 1, 2013 at 11:41
  • And the issue is that they are not being sorted in the correct order? Commented Mar 1, 2013 at 11:43
  • 1
    What is your question? Are you aware that the string "1" is < than "19"? In other words, are you aware that numeric sort and lexicographic sort gives different results? Commented Mar 1, 2013 at 11:44

4 Answers 4

2

The requirement is very specific and unusual, Of course, there is no standard method for it. But you could provide a custom comparator:

Arrays.sort(rows, new Comparator<String>()
{
    public int compare(String o1, String o2)
    {
         return Integer.parseInt(o2) - Integer.parseInt(o1);
    }
});

And you have to decide what to do if a string has unparsable integer and deal with the NumberFormatException.

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

1 Comment

Thanks for the reply. So I have used an if loop to see if the input string contains an integer or not and that being if(rows[0].matches("\\d+")){Arrays.sort(rows, new Comparator<String>()...}.
1

The problem is that the natural order of strings is not the same as that of numbers: "1" < "11" < "2" as opposed to 1 < 2 < 11. It's a matter of writing your own comparator:

    Arrays.sort(rows, new Comparator<String>() {
        public int compare(String o1, String o2) {
            return -(Integer.parseInt(o1) - Integer.parseInt(o2));
        }
    });

It will use the integer value of each string - it will crash if any of the strings isn't an integer -, and it will give the reverse order since it returns - the normal value. It is also inefficient because it parses every string in every comparison.

Comments

0

If you want a sort in numeric order, you need a numeric type, not String. You can create an array of Integer and sort that.

Comments

0

The sorting is done using alphabeticaly Strings. the String "3" is greater than "14" since the char '3' is greater than the char '1' (in reverse order).

you need either pass an own comparator witch converts the string to intergers before comparing them or have the intergers formatet so they have same number of digits and leading 0.

If having words an number as String in the same array than you cant get the number to orderd numericaly.

Comments

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.