java : Sort Files based on Creation Date

前端 未结 2 643
野性不改
野性不改 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.

    0 讨论(0)
  • 2021-01-15 02:31

    Try flipping the comparison order:

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

    This works for me testing locally just now.

    0 讨论(0)
提交回复
热议问题