• Post Reply Bookmark Topic Watch Topic
  • New Topic

unable to sort Alphanumeric values  RSS feed

 
sai prasanna
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I had a small query regarding the following

When sorting the following values by .sort() it is not displaying the values in ascending order i.e
it is taking 10 as 1 and displaying 10 first rather than 2

s1[0]="BS EN 60704-2-10:2002";
s1[1]="BS EN 60704-2-2:2003";

Thanks in Advance
saiprasanna
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because this compares every characters when sorting.
you should make your own Comparator and break the string into relevant pieces.
 
sai prasanna
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satou
 
sai prasanna
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the values are in different format as follows then also should i write my own comparator

BS EN 60704-1:1995

BS 1016-104.4:1998, ISO 1171:1997

BS 2782-3:Method 323B:1996, ISO 6721-5:1996

thanks in advance
saiprasanna
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sai prasanna:
but the values are in different format as follows then also should i write my own comparator

BS EN 60704-1:1995

BS 1016-104.4:1998, ISO 1171:1997

BS 2782-3:Method 323B:1996, ISO 6721-5:1996

thanks in advance
saiprasanna


Yes. If the standard String compare method doesn't give you the ordering you want then write a Comparator that does.

How about this for the custom method:
  • Use String.split() to get a String array for each long String to be compared. For instance, the two from your initial post would be "BS", "EN", "60704", "2", "10", "2002" and "BS", "EN", "60704", "2", "2", "2003".
  • In a loop, compare corresponding elements in each array.

  • - Try converting the given element in each array to an int. If neither conversion throws a NumberFormatException, compare the two ints; else compare them as strings.
    As soon as a comparison provide a lt, eq, gt decision you're done. If all corresponding parts are equal and neither array has extra pieces, the two are equal.

    Note: This would ignore differing puncuation/spacing. "BN 123" would be equal to "BN:-:===123". To change this you could include Strings of delimiters in your array. For instance, those two might be "BN", " ", "123" and "BN", ":-:===", "123".

    Cool?

    STANDARD NOTE ABOUT equals() AND hashCode():
    If you create a custom class for these numbers and override the equals() method, make sure that hashCode() returns equal ints for any two objects that are equal. i.e. if a.equals(b) then a.hashCode() had darn well better == b.hashCode().
    [ September 07, 2006: Message edited by: Ryan McGuire ]
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!