• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sorting Collections

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I came across this when I try to practice the example in SCJP 5.0 Study Guide by Kathy and I added some lines to is as well.

Program:-
ArrayList <String> test=new ArrayList<String>();
String s="hi";
test.add(s);
test.add("String");
test.add("EEE");
test.add("ABC");
System.out.println(test);
Collections.sort(test);
System.out.println(test);

and this is the output I get

[hi, Sting, EEE, ABC]
[ABC, EEE, Sting, hi]

Do any of you guys no the answer for this?

Cheers,
J
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
De, I'm not sure what your question is. It seems you have some code and it is behaving as expected so what are you wanting it to do?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do any of you guys no the answer for this?



Strings are sorted based on unicode values. For letters and numbers, it is the same as the ASCII values. In the first printout, it is unsorted. In the second, it is sorted.

Henry
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with the way the String.compareTo() method has been implemented. Basically the natural order imposed by the compareTo() implementation is based on the Unicode value of the individual characters in the String. The unintuitive (non-alphabetical) ordering of the output of your code sample is simply the result of upper case characters preceeding lower case characters.
If you want take character case out of the equation when sorting the List, you could either make sure the List only contains lower case or upper case Strings, or sort the List using a Comparator implementation that makes use of the String.compareToIgnoreCase() method.

Edit: Must. Type. Faster.
[ December 02, 2008: Message edited by: Jelle Klap ]
 
Jade Lucky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys thanks for quick reply ,
The first println prints unsorted collection while second one prints sorted one.This the output I get
[ABC, EEE, Sting, hi]

what I meant was it should be sorted as
[ABC, EEE, hi,Sting]

Because I have used
String s="hi" ,its not sorting properly.

Do you have any inkling what can be the reason for this?

Thanks.
D
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The capital 'S' comes before the lowercase 'h' in unicode values, so it will come before "hi". Take Jelle's suggestion to use a Comparator using the String.compareToIgnoreCase() method if you want the output to be in alphabetic order regardless of upper/lowercase.
 
Jade Lucky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jelle,
Thanks for the reply.

It works.

Cheers,
De
 
reply
    Bookmark Topic Watch Topic
  • New Topic