Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

string

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
like image 382
user3792817 Avatar asked Dec 12 '25 10:12

user3792817


1 Answers

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.

like image 149
ThatGuy343 Avatar answered Dec 14 '25 01:12

ThatGuy343