Sorting question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Sorting question

  1. #1
    Join Date
    Feb 2010
    Posts
    121

    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

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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.

  3. #3
    Join Date
    Feb 2010
    Posts
    121

    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

  4. #4
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,422

    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

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Sorting question

    Quote Originally Posted by peahead View Post
    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.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Sorting question

    Quote Originally Posted by peahead View Post
    I just looking at the easiey way to sort.
    It's easiest to sort on "score" before you generate the Strings.

  7. #7
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,422

    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

  8. #8
    Join Date
    Feb 2010
    Posts
    121

    Re: Sorting question

    Quote Originally Posted by dlorde View Post
    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

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: Sorting question

    Quote Originally Posted by peahead View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center