Results 1 to 11 of 11
Thread: Sorting strings in java?
- 08-30-2008, 04:29 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 8
- Rep Power
- 0
Sorting strings in java?
Hi all,
I'd like to know how to sort strings in java. Usually in C I'd do something like this:
Java Code:for(i=0;i<length-1;i++) { for(j=0;j<length;j++) { if(strcmp(name[i],name[j])>0) { strcpy(tmp,name[i]); strcpy(name[i],name[j]); strcpy(name[j],tmp); } } }
- 08-30-2008, 04:32 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Store them in a TreeSet, it ordered all elements in natural order. Or use Array.sort() on a string array.
Last edited by Eranga; 08-30-2008 at 04:35 PM.
- 08-30-2008, 04:40 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Here is a simple example.
Java Code:/** * * @author Eranga Tennakoon */ public class SortArrays { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String[] arr = new String[]{"Java", "Forums", "Sun", "Apache", "JSP", "AJAX", "XML"}; new SortArrays().sortStrings(arr); } private void sortStrings(String[] arr) { java.util.Arrays.sort(arr); for(int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
- 08-30-2008, 05:06 PM #4
The code in java would be translated one for one line of code.
There are comparable methods to the C strcmp function in the String class. Java is a bit more wordy. Read the API doc for String.
Use assignment statement vs using strcpy for the rest
- 08-31-2008, 07:24 AM #5
Store them in any Collection implementation that has a "sort()" member function, and call it.
- 08-31-2008, 07:38 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
In a TreeSet it's automatically happens, right?
- 08-31-2008, 07:50 AM #7
I think so, I'd have to check the javadocs. But you may need a comparitor of you want to support sorting in a special way.
So the general answer is use a Collection, and a TreeSet is one.
I don't know any of the asian languages, but even European ones have some non-obvious rules. For example, in Spanish, the "ch" pair is sorted as if it was a single character and sorts after "C" alone.
Doing "strings" properly in a multi-language environment is a bit of a challenge. Java lets you do it "right" but its not all automatic.
- 08-31-2008, 08:18 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Yep you are right? If we think in English-only mindset sorting strings is easy. But in different languages, it wont.
In TreeSet internally elements are sorted in natural order, and output as an array. See the example bellow.
Java Code:public static void main(String[] args) { TreeSet at = new TreeSet(); at.add("Java"); at.add("Name"); at.add("NetBeans"); at.add("AJAX"); System.out.println(at); }
- 08-31-2008, 12:37 PM #9
For different languages,other than English, we i think we should write a unicode convert function,and then creating unicode strings in TreeSet,maybe then it will sort.I will try to make a little test application now in Eclipse with Russian and Hebrew languages .
- 09-01-2008, 01:01 AM #10
I checked the TreeSet sorts the array only for English characters,for german with umlauts it sends the strings to the end of the TreeSet,in hebrew it doesn't sort it at all and the same in Russian,but the Arrays.sort(<some array of strings>) sorts everything,that means TreeSet doesn't support unicode while sorting.
So in TreeSet the strings in other languages should be unicoded first and then sorted with Comparator
Java Code:import java.util.*; public class JavaUnicode { private TreeSet set=new TreeSet(); public JavaUnicode(){ set.add("דויד"); set.add("קריש"); set.add("גרושה"); set.add("אלמן"); System.out.println(set); String[] s=new String[]{"דויד","קריש","גרושה","אלמן"}; //String[] s=new String[]{"David","Allan","Nick"}; Arrays.sort(s); for(int i=0;i<s.length;i++){ System.out.println(s[i]); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new JavaUnicode(); } }
the output:
Java Code:[אלמן, גרושה, דויד, קריש] אלמן גרושה דויד קריש
Last edited by serjant; 09-01-2008 at 01:06 AM.
- 09-01-2008, 05:14 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
So the java.util.Arrays.sort() is the best choice in sorting. It's easy to work on, because no need to deal with UNICODE conversions and so on.
Similar Threads
-
Sorting CachedRowset
By Sayed in forum Advanced JavaReplies: 0Last Post: 07-18-2008, 01:14 PM -
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 08:39 PM -
sorting problem
By mcal in forum New To JavaReplies: 1Last Post: 02-14-2008, 09:13 AM -
Help with Sorting Program
By rhm54 in forum New To JavaReplies: 3Last Post: 01-25-2008, 11:08 PM -
Heap Sorting
By kesav2005 in forum New To JavaReplies: 1Last Post: 11-13-2007, 05:04 PM
Bookmarks