If that whole list you posted is a single String, you'll need to separate it into individual items.
The output you've posted differs from the input in more than order - the colon after the initial number is missing. To get this result you need more than a sort.
If you could be more precise about what your input is and what you require for output, it would be possible to give you more specific answers.
Questions are the important thing, answers are less important. Learning to ask a good question is the heart of intelligence. Learning the answer---well, answers are for students. Questions are for thinkers...
R. Schank
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
The string is built up line by line (usually in seconds) from the code I supplied which is in a while loop - possibly we could sort at source before it is sent to my JTextArea? Really, I just looking at the easiey way to sort. Thanks
What form is the data in? Strings in an array or a container of some type?
Or is it all in one String that needs to be parsed into separate records before sorting?
The Arrays class has several sort() methods that could work for you.
So it's not a String as you said previously, but a List of Strings?
The string is built up line by line (usually in seconds) from the code I supplied which is in a while loop - possibly we could sort at source before it is sent to my JTextArea? Really, I just looking at the easiey way to sort. Thanks
I can't quite make sense of what you're asking. Try to imagine I'm a stranger on an internet forum, who has no idea about your application...
From what I can make out, you have a List of Strings where each String contains a number (score), a colon, and what looks suspiciously like a chess move. You want the list sorted by the initial number of each String... yes?
If you're only going to display the List, you can write a Comparator that compares the characters before the colon as a number (substring and parse to int), and use the Collections.sort(..) method to sort the List.
If the List items (Strings) are to be used in processing, I'd recommend converting them to objects of a class with separate fields for score and move(?). Then you can either have that class implement Comparable or write a score Comparator.
Precise language is not the problem. Clear language is the problem...
R. Feynman
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
So it's not a String as you said previously, but a List of Strings?
If you're only going to display the List, you can write a Comparator that compares the characters before the colon as a number (substring and parse to int), and use the Collections.sort(..) method to sort the List.
Thank you. This is exactly what I want to do. Now that I know it is possible I am happy to research this method.
I wouldn't do that. You get a brittle solution. It's better to sort on "score" before you generate the list of Strings for printing.
Otherwise you've got an almost guaranteed bug down the line. Someone changes the printing format and the sorting "unexpectedly and mysteriously" doesn't work anymore.
Bookmarks