• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sorting Issue Help Needed Badly

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.








 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks correct. What's the issue?
 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i learnt 2 comes after 1 not 19, 20 comes after 19 not 2 ...
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh there i am a dunce I will try a aplhanumeric comparator
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic