Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Issue Help Needed Badly

 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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.








 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks correct. What's the issue?
 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Oh there i am a dunce I will try a aplhanumeric comparator
 
jali pingle
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic