Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does listFiles() not guarantee order?

Tags:

java

file

This seems like a strange thing to say.

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Why can't the order be guaranteed? What one gets when one ls could, for example be a reasonable default.

Is this something that was decided for (performance?) reason, or perhaps where is some deeper truth out there?

like image 649
James Raitsev Avatar asked Feb 19 '23 15:02

James Raitsev


1 Answers

As far as I know, there is no natural order that files gravitate toward. If you say alphabetic, then with which locale/collating sequence? Do all OSes that java runs on support that order intrinsically? If not, then the API would need to resort whatever was received from the OS. Since it is likely that you the API client 1) may not care or 2) will want something different, it makes much more sense to just deliver the files with no guaranteed order rather than doing a wasted sort. If you care, you can do the sort.

like image 83
walrii Avatar answered Feb 27 '23 04:02

walrii