This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I sort String arrays?

 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to sort a String array? I am using tree which gives a list of picked nodes through request ParameterValues. I am storing it to a String array. Can I sort these values?

Ananth Chellathurai
 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is easy since String implements the Comparable interface; you can use s1.compareTo(s2).
 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what should I compare to? I just want my string array to be sorted alphabetically.

Ananth
 
arulk pillai
Author
Ranch Hand
Posts: 3380
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



If you have special requirements


class test {

public static void main(String[] args) {
String words[] = { "Good", "Bad", "Ugly" };
Comparator<String> best = new Comparator<String>() {
public int compare(String s1, String s2) {
return s2.charAt(1) - s1.charAt(1}
};
Arrays.sort(words);
System.out.println(words[0]);
System.out.println(words[1]);
System.out.println(words[2]);
}
}

[ September 15, 2008: Message edited by: arulk pillai ]
 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Arul. I will use this in my implementation.

Ananth Chellathurai
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to use String.CASE_INSENSITIVE_ORDER if you want to sort it while ignoring the case.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic