Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:25:46+00:00 2026-06-17T09:25:46+00:00

I have a file list which I want to sort and extract the top

  • 0

I have a file list which I want to sort and extract the top 3 last modified.

Constraint: I cannot use Java 7 due to compatibility issues on downstream apps

My current options

Solution 1

File[] files = directory.listFiles();    
Arrays.sort(files, new Comparator<File>(){
    public int compare(File f1, File f2)
    {
        return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
    } });

Solution 2

public static void sortFilesDesc(File[] files) {
  Arrays.sort(files, new Comparator() {
    public int compare(Object o1, Object o2) {
      if ((File)o1).lastModified().compareTo((File)o2).lastModified()) {
        return -1;
      } else if (((File) o1).lastModified() < ((File) o2).lastModified()) {
        return +1;
      } else {
        return 0;
      }
    }
  });
}

Problem

The above two solution takes more time to execute & memory. My file list consists of some 300 tar files with 200MB size each. so it is consuming more time & memory.

Is there is any way to efficiently handle this?

Every compare operation uses a file object which is of high memory is there is any way to release the memory and handle this effectively?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:25:47+00:00Added an answer on June 17, 2026 at 9:25 am

    You can do it much faster.

    Arrays.sort(…) uses “quick sort”, which takes ~ n * ln(n) operations.

    This example takes only one iteration trough the whole array, which is ~ n operations.

    public static void sortFilesDesc(File[] files) {        
        File firstMostRecent = null;
        File secondMostRecent = null;
        File thirdMostRecent = null;
        for (File file : files) {
            if ((firstMostRecent == null)
                    || (firstMostRecent.lastModified() < file.lastModified())) {
                thirdMostRecent = secondMostRecent;
                secondMostRecent = firstMostRecent;             
                firstMostRecent = file;
            } else if ((secondMostRecent == null)
                    || (secondMostRecent.lastModified() < file.lastModified())) {
                thirdMostRecent = secondMostRecent;
                secondMostRecent = file;
            } else if ((thirdMostRecent == null)
                    || (thirdMostRecent.lastModified() < file.lastModified())) {
                thirdMostRecent = file;
            }
        }
    } 
    

    On small numbers of files you won’t see much difference, but even for tens of files the difference will be significant, for bigger numbers – dramatic.

    The code to check the algorithm (please put in a correct files structure):

    package com.hk.basicjava.clasload.tests2;
    
    import java.io.File;
    import java.util.Date;
    
    
    class MyFile extends File {
    
        private long time = 0; 
    
        public MyFile(String name, long timeMills) {
            super(name);
            time = timeMills;
        }
        @Override
        public long lastModified() {
            return time;
        }
    }
    
    public class Files {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            File[] files = new File[5]; 
            files[0] = new MyFile("File1", new Date(2013,1,15, 7,0).getTime());
            files[1] = new MyFile("File2", new Date(2013,1,15, 7,40).getTime());
            files[2] = new MyFile("File3", new Date(2013,1,15, 5,0).getTime());
            files[3] = new MyFile("File4", new Date(2013,1,15, 10,0).getTime());
            files[4] = new MyFile("File5", new Date(2013,1,15, 4,0).getTime());
            sortFilesDesc(files);
        }
    
        public static void sortFilesDesc(File[] files) {        
            File firstMostRecent = null;
            File secondMostRecent = null;
            File thirdMostRecent = null;
            for (File file : files) {
                if ((firstMostRecent == null)
                        || (firstMostRecent.lastModified() < file.lastModified())) {
                    thirdMostRecent = secondMostRecent;
                    secondMostRecent = firstMostRecent;             
                    firstMostRecent = file;
                } else if ((secondMostRecent == null)
                        || (secondMostRecent.lastModified() < file.lastModified())) {
                    thirdMostRecent = secondMostRecent;
                    secondMostRecent = file;
                } else if ((thirdMostRecent == null)
                        || (thirdMostRecent.lastModified() < file.lastModified())) {
                    thirdMostRecent = file;
                }
            }
            System.out.println("firstMostRecent : " + firstMostRecent.getName());
            System.out.println("secondMostRecent : " + secondMostRecent.getName());
            System.out.println("thirdMostRecent : " + thirdMostRecent.getName());
        } 
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTree which contains file/directory, i want to get a list which
I have a web.xml which looks like: <web-app> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <security-constraint> <web-resource-collection> <web-resource-name>Default</web-resource-name>
I have an input file that I want to sort based on timestamp which
I have a file, list.txt which contains a list of words. I want to
I have a make rule which generates a dependencies file for a list of
I have a long list of files and file extensions which I would like
I have a php file list.php <?php $arr=array('444','555'); echo var_export($arr); ?> Now I want
I have a manifest file which is just a list of newline separated filenames.
I have a file that contain list of files I want to archive with
I want to create a list of those files in a directory which have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.