java.io.File.list() giving different order as the folders created sequentially [duplicate]

StackOverflow https://stackoverflow.com/questions/21275228

  •  01-10-2022
  •  | 
  •  

Question

java.io.File.list() giving different order as the folders created sequentially

While creating the Temporary folders with name as

"Temp" + auto-incremented number 
e.g. Temp1, Temp2, Temp3...Temp10, Temp11 and so on. 

But when I use java.io.File.list() to get the folder list to further processing in order as the folder created,

but its giving as Temp1, Temp10, Temp11...Temp19, Temp2, Temp20, Temp21 and so on.

I also tried Arrays.sort(files); but no change in order of folder list

Please tell how to get the same order list as the folders created sequentially?

Was it helpful?

Solution

It's because the way how java compares strings by default in java (lexicographic order). You can define your own comparator for File objects and sort.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top