Results 1 to 6 of 6
  1. #1
    evermore is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default customize sorting

    if u wanted to sort string in a ridiculous way such as placing the rule
    a - z comes before A - Z

    example Aab , aAb

    ordering would result in aAb then Aab

    obviously you can hard code this. but i'm looking for something more of a shortcut

    anything that lets me state or modified small change that will allow me to do this

    have read comparable interface, Collection, comparator

    looking for general method that allows u to state rules for sorting and it will sort

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    If you've read about the Comparator interface, then you know that it is the way to specify your "rules for sorting".

    -Gary-

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Perhaps you could say more precisely what you are trying to do.

    gcalvin is quite right about Comparator being the "general method that allows [yo]u to state rules for sorting". Particular implementations of Comparator might be appropriate for the specific sorts you have in mind.

    For example the example you gave looks like a lexical sort: where you examine the elements of a pair of sequences in turn and when you first discover a difference, use that to determine the ordering of the sequences. The "rule" for such a Comparator could be as simple as an array specifying the ordering you want individual characters to have. Setting the array would give your Comparator the precise behaviour you wanted.

    A more elaborate example of a Comparator whose behaviour is specified by rules (specifically designed for locale sensitive sorting of strings) is RuleBasedCollator. The example you gave would be specified with rules that give the "tertiary" or case distinction, so called because it is typically applied after letter or accent difference. Don't know if this qualifies as a "shortcut" though...

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Yes, actually pbrockway2's answer was quite helpful, while mine was kind of snide and unhelpful. Sorry about that.

    -Gary-

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Actually your reply was what I was going to post some time ago! I mean the OP asked for the general method and that's ... a Comparator. I don't think it's snide to point that out.

    I know it's difficult for people to decide how precisely they should describe a problem, but it's a pity there hasn't been a response to my suggestion that a little more detail would be helpful here. This being Java it's a fair bet that the answer will be a Comparator: but how do you implement it? For that it would be good to know more about how it's going to be used.

  6. #6
    evermore is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 03-27-2010, 09:01 AM
  2. How to Customize Jfreechart value axis?
    By neeraj.singh in forum AWT / Swing
    Replies: 0
    Last Post: 02-16-2009, 08:57 AM
  3. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 10:30 AM
  4. Sorting CachedRowset
    By Sayed in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2008, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •