3

How can I sort an array and print the values in descending order?

say the example array is: ["1a","1b","1c"]

they have numbers before the first character alphabet values, i want to print:

1c
1b
1a
2
  • If it's a question of printing only, you might as well sort and then loop from last index to first when printing. Commented Jul 1, 2014 at 7:24
  • well its kinda specific to the pattern of the strings i need to do this with and they are being added in different orders. Commented Jul 1, 2014 at 7:26

1 Answer 1

4

What you are asking is to sort an array in reverse order.

Basically you do this by reversing Arrays.sort() which is ascending.

String [] testArray = {"1a", "1b", "1c"};

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

for (String str : testArray) {
    System.out.println(str);
}

Output is,

1c
1b
1a

You can test this here, https://ideone.com/q1OGBD.

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

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.