James Raitsev
James Raitsev

Reputation: 96431

Why does listFiles() not guarantee order?

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?

Upvotes: 4

Views: 1249

Answers (1)

walrii
walrii

Reputation: 3522

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.

Upvotes: 8

Related Questions