String[] myArray = {"Player 30","Player2 35"};

This statement


will sort myArray by Letters.

aPlayer 30
bPlayer 35

Problem: Is there a way to sort myArray by numbers from highest to lowest?
The output should be:

bPlayer 35
aPlayer 30

I even tried foolish things like.

String[] myArray = {"30 Player","35 Player2 "};

by sorting this using


and reverse it so it will output

35 Player2
30 Player

But if myArray contains this Element 9 Player it will be the first output. I know this sucks hehe trying to fool.

Back to the original problem.
Help is much appreciated thank you.

Specify your own comparator.

Do the parsing as mentioned in your other thread then return the results of the compare between the two numbers.

You need to implement either Comparable or Comparator. Look up Arrays.sort in the javadoc and you will find links to these.

I assume this is a continuation of the recent thread? In that, you said scores should be in an integer array, and I told you how to convert the strings to integers.
If you make it an array of ints it will sort numerically.

However, if you want to sort your data by score, the separate arrays of names and scores is a very poor way to go because sorting one of them will just leave it out of synch with the other. )You didn't mention this in the earlier thread).
I suggest you look at creating a little class to hold a name and a related score - you can create instances directly from the input String - and put these objects into an array (or ArrayList). You can then specify your own comparator as previously suggested to sort by score, alpha by name, or whatever. This would be the proper Object Oriented or Java way to do it .

Edited 6 Years Ago by JamesCherrill: n/a

*Nosebleed* *sniffs* Thank you for you reply . I'm just new to java so i will have a hard time figuring out what are you talking about. Maybe one by one i can do what you suggested.

Edited 6 Years Ago by DoEds: n/a

Sorry, I realise that's quite a block of text to take in in one go, but it is structured as a series of specific points, in the order you need them, so just take it slowly and you'll be OK. If I'm not around (I'm going out soon) there are loads of other people here who are familiar with what I described and can help you.

This article has been dead for over six months. Start a new discussion instead.