-
August 2nd, 2011, 09:02 AM
#1
Sorting question
What would be the best way to sort this string into descending order by score, i.e:
from:
-4: a7a5
-3: b7b6
-6: f7f5
1: c7c6
2: d7d6
12: e7e6
13: d7d5
10: g8f6
to:
13 d7d5
12 e7e6
10 g8f6
2 d7d6
1 c7c6
-3 b7b6
-4 a7a5
-6 f7f5
Will comparator work or is there an element of parsing involved to strip out the score, sort and then put back together again?
For info, this is the code that produces the string
Code:
jcb.showMessageDisplayAnalysis(score + ": " + move);
Thanks
-
August 2nd, 2011, 09:15 AM
#2
Re: Sorting question
It's not clear from what you posted.
A Comparator is a way of parsing the elements...
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.
-
August 2nd, 2011, 10:51 AM
#3
Re: Sorting question
Sorry about that. I used Excel to sort the order but it removed the colon which I didn't notice.
So, to clarify, this is my original list:
-4: a7a5
-3: b7b6
-6: f7f5
1: c7c6
2: d7d6
12: e7e6
13: d7d5
10: g8f6
and I need it changed into descending order based on score, as follows:
13: d7d5
12: e7e6
10: g8f6
2: d7d6
1: c7c6
-3: b7b6
-4: a7a5
-6: f7f5
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
-
August 2nd, 2011, 11:10 AM
#4
Re: Sorting question
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.
Norm
-
August 2nd, 2011, 11:22 AM
#5
Re: Sorting question
Originally Posted by peahead
So, to clarify, this is my original list:
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.
-
August 2nd, 2011, 11:26 AM
#6
Re: Sorting question
Originally Posted by peahead
I just looking at the easiey way to sort.
It's easiest to sort on "score" before you generate the Strings.
-
August 2nd, 2011, 11:28 AM
#7
Re: Sorting question
If these are chess moves, the score is generated for the move. Then sorted to put the strongest move first.
Perhaps a SortedSet would work.
Last edited by Norm; August 2nd, 2011 at 11:31 AM.
Norm
-
August 3rd, 2011, 07:34 AM
#8
Re: Sorting question
Originally Posted by dlorde
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.
Cheers
-
August 3rd, 2011, 10:20 AM
#9
Re: Sorting question
Originally Posted by peahead
This is exactly what I want to do.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|