user3792817
user3792817

Reputation: 33

Java how to sort string array based on order values were placed?

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

Upvotes: 3

Views: 137

Answers (1)

ThatGuy343
ThatGuy343

Reputation: 2424

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.

Upvotes: 4

Related Questions