This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 19651
    
  18

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: 19651
    
  18

Please Use Code Tags
 
wood burning stoves
 
subject: Sorting Issue Help Needed Badly
 
Similar Threads
Sorting Asciibetically
Sorting two arrays...
Sort the Array in reverse order
String Input and Bubble Sort
Load Main class error!!! trying to sort the names inputted in alphabetical order...please help!