• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to insure the list of filenames copied to an array is sorted in the same order than directory?  RSS feed

 
Jon Hedge
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a folder containing files/pictures (under windows 7) displayed as large icons in an ascendent order like picture_a.png, picture_b.png and picture_c.png.

Using the code bellow I would like to ensure that these files/pictures names will be copied into the varaiable s in the same order than they are in the folder.

Thanks in advance for your help,

 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can sort them any way you like, but in your Windows computer they aren't stored in any meaningful order in the directory. Not only that, the tools you use to look at the files in the directory will automatically sort the entries in some sequence, thus hiding the fact that internally the files are stored in no particular order.

However if you want to sort the files in alphabetical order by their names, you could certainly do that. That's what your example looked like. So is that what you actually want to do?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The File class implements Comparable, so you can sort the array without doing anything special. Look at the methods in class java.util.Arrays
 
Jon Hedge
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:However if you want to sort the files in alphabetical order by their names, you could certainly do that. That's what your example looked like. So is that what you actually want to do?

Yes, exactly since at some points in a loop of the code I would like to delete the files/pictures one by one from the folder once each of the file/picture has been processed.

I've tried the code below but I obtain an error :

 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing to do when you get an error is to read it. Normally the error message is supposed to help you fix whatever is wrong, or at least to deal with it.

If you don't understand what it's trying to tell you -- which is a perfectly normal thing to happen to a beginner -- then you can certainly ask that here. But "I obtain an error" isn't a good way to ask that, because we don't know what the error says. And it's important to read the error message.
 
Jon Hedge
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error was just highlighted in the IDE since I did not build the code. The error message (after building) is the following :

error: cannot find symbol
sorted_s = sort(s);
symbol: method sort(String[])

 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Hedge wrote:The error was just highlighted in the IDE since I did not build the code. The error message (after building) is the following :

error: cannot find symbol
sorted_s = sort(s);
symbol: method sort(String[])


It's looking for a method named "sort". You have not provided this method. If you are expecting to use a sort method available in the Java library you'll have to look up which class it is in and invoke it according to the documentation. You might see if the Arrays class has anything to offer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!