Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrange in ascending order

 
Panday Manako
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
How can you arrange words in ascending order ignoring the case?
I always get an order in which words that start with an uppercase letter are on top.
example: Z,a,b,c,E
result: E,Z,a,b,c
here's my partial code
Collections.sort( ListOfNames );
Iterator firstName = ListOfNames.iterator();
while ( firstName.hasNext() )
{
System.out.println( firstName.next() );
}
Thanks!
Panday
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Panday
What you need to do is sort the Strings ignoring case. If your going to use the sort method then you might want to use the overloaded form that takes a list and a comparator. The comparator lets you define your own comparison criteria the sort method then uses that to sort your list.
Your comparator would look something like this:

This is an adaptation of an old utility class I had made and used inner classes to facilitate this.
Then in your code just send the sort method your lsit and the comparator.
hope that helped

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Panday Manako
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave!
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections.sort( ListOfNames , String.CASE_INSENSITIVE_ORDER);
------------------
Cindy Glass
Sun Certified Programmer for the Java� 2 Platform
Co-author of Java 2 Certification Passport
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic