java : Sort Files based on Creation Date

前端 未结 2 642
野性不改
野性不改 2021-01-15 02:02

I am trying to read the latest 10 files based on their creation date .

I tried with this code , but it isn\'t working , i mean , it doesn\'t show the new file nam

2条回答
  •  有刺的猬
    2021-01-15 02:17

    At least in regular Java version you compare files in the wrong (ascending) order. I multiplied result by -1 and I am seeing latest files first:

    return -1* (new Long(((File)o1).lastModified()).compareTo(new Long(((File) o2).lastModified())));
    

    With timestamps the larger one corresponds to the newer file.

提交回复
热议问题