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 Sorting Issue Help Needed Badly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting Issue Help Needed Badly" Watch "Sorting Issue Help Needed Badly" New topic
Author

Sorting Issue Help Needed Badly

Dileep Malayanur
Greenhorn

Joined: May 27, 2008
Posts: 17
Hello All,
I am having a small piece of code in my project that retrieves file names from a directory applying filename filter. Well i am happening to get them fine in sorted order. But when i implement that program in other machine it happens that all the file names are in unsorted order. I applied several sorting techniques Collator, SortedSet etc. no luck</stro>
*Any could help me on this. By the way my development machine has JDK 1.6 while implementing system has only JRE1.5 which comes embedded with jBase. Consider i have nothing to do with jBase thats another team's project.









Dileep Malayanur
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

The sort order is determined by the underlying file system, and that does not guarantee a sort order. Often it uses file creation date.

Your code is... impressive, but what's wrong with a simple Arrays.sort() call using a custom (or preexisting) comparator? For instance, to sort on name ignoring the case:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dileep Malayanur
Greenhorn

Joined: May 27, 2008
Posts: 17
Well as i said i even tried Arrays.sort also with String.CASE_INSENSITIVE_ORDER no luck. Well you are right i suppose because the development machine has FAT32 and the implementing machine has NTFS. Oh no whats next.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I don't understand - if you use Rob's approach its Strings you are sorting, not Files. Strings are not going to be impacted by the underlying file system.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Dileep Malayanur
Greenhorn

Joined: May 27, 2008
Posts: 17
Well for a test i manually altered the string array order and fed to the sorting algorithm here is the output that i got for SortedSet








>
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

That looks correct. What's the issue?
Dileep Malayanur
Greenhorn

Joined: May 27, 2008
Posts: 17
Well i learnt 2 comes after 1 not 19, 20 comes after 19 not 2 ...
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not if your sort is alphabetic, which it is. Normally, AAB comes before AB, simmilaraly 112 comes before 12. If you want it to sort differently you'll need to implement this in a custom Comparator.
Dileep Malayanur
Greenhorn

Joined: May 27, 2008
Posts: 17
Oh there i am a dunce I will try a aplhanumeric comparator
jali pingle
Greenhorn

Joined: Mar 19, 2009
Posts: 1
public static void sortStringsInAscending(String[] myArray)
{
String[] array = myArray;
System.out.println("***** before **********************");
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]);
}
for(int m=0;m<myArray.length-1;m++)
{
for(int n=m+1;n<myArray.length;n++)
{
if(myArray[m].length()>= myArray[n].length()&& myArray[m].compareTo(myArray[n])> 0||myArray[m].length()> myArray[n].length())
{
/* swap */
String temp=myArray[m];
myArray[m]=myArray[n];
myArray[n]=temp;
}
}
}
System.out.println("*****after **********************");
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]);
}
}
or you can use this.


Everything is possible.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Please Use Code Tags
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting Issue Help Needed Badly
 
Similar Threads
Load Main class error!!! trying to sort the names inputted in alphabetical order...please help!
Sorting two arrays...
String Input and Bubble Sort
Sort the Array in reverse order
Sorting Asciibetically