File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes problem with sorting Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "problem with sorting Strings" Watch "problem with sorting Strings" New topic
Author

problem with sorting Strings

Satish Palli
Greenhorn

Joined: Jun 13, 2006
Posts: 3
Hi all , I have a problem with sorting Strings.

My problem is i have a tree of strings like
1 RootOne
1.1 SubIetm1
1.2 SubIetm2
1.3 SubIetm3
1.4 SubIetm4
....
....
1.9 SubIetm9
2 RootTwo


Like this , what happens is when the SubItems are more than 10 like

1.10 SubItem10
1.11 SubItem11
these items are placed before
1.2 SubItem2

like
1.1 SubIetm1
1.10 SubItem10
1.11 SubItem11
1.2 SubIetm2
1.3 SubIetm3
1.4 SubIetm4
....
....
1.9 SubIetm9

the total item that is 1.1 SubItem1 is String.

Could any one help me in this regard
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
int compareTo(String anotherString)
Compares two strings lexicographically.

if you use this then it will return an int. Use this int to compare the string before it.

So if S1.compareTo(String S2) >0 then S1 is larger.
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
You'll have to write your own comparator and pass it to the sort() method. In your comparator, you'll extract the digits before the dot, convert them to an int and compare them. Then, if necessary, do the same for the digits following the dot (if there aren't any, treat that as a zero).
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: problem with sorting Strings
 
Similar Threads
swing/awt compatibility ?s
compiling code using different versions (jdk 1.3 and jdk 1.4)
GB vs the USA
clrscr() function?
Java development on Windows-95