Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sorting string like windows file name sorting

807591Apr 24 2008 — edited Apr 25 2008
Hi everybody, I have a simple question.

I was trying to sort some file name in java

Let say we have a list of string which are :

1.txt
a.txt
2.txt
11.txt
a(1).txt
a(11).txt
a(2).txt

If i convert those strings as file names, and sort it by file name in the windows explorer the result is

1.txt
2.txt
11.txt
a(1).txt
a(2).txt
a(11).txt
a.txt

But if i enter those strings into an Arraylist and use Arrays.sort or Collections.sort, the result is

1.txt
11.txt
2.txt
a(1).txt
a(11).txt
a(2).txt
a.txt


Is there a way to achieve the string sort similar to windows rather than aplhabetically like the default sort of Arrays/Collection.

I have done some searching but only found problem regarding to different language character sorting which can be achieved using Collator but this was not my case. Has anybody encounter this issue ?

Any help is greatly appreciated

regards

Hendra Effendi
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 23 2008
Added on Apr 24 2008
7 comments
1,915 views