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 875725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:19:08+00:00 2026-05-15T11:19:08+00:00

I have a simple Java program which reads a file directory and outputs a

  • 0

I have a simple Java program which reads a file directory and outputs a file list.
I sort the files by name:

String [] files = dirlist.list();
files = sort(files);

My problem is that it sorts by name in a different way than Windows Explorer does.

For instance if I have these files: abc1.doc, abc12.doc, abc2.doc.

Java will sort like this:

abc1.doc
abc12.doc
abc2.doc

When I open the folder in Windows Explorer, my files are sorted like this:

abc1.doc
abc2.doc
abc12.doc

How can I make Java sort my files like in Windows Explorer?
Is this a Windows trick?

  • 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-05-15T11:19:08+00:00Added an answer on May 15, 2026 at 11:19 am

    Windows Explorer uses its own algorithm. In order to sort your files like Explorer does, you should create a custom Comparator to do the trick:

        public class WindowsExplorerStringComparator implements Comparator<String>
        {
          private String str1, str2;
          private int pos1, pos2, len1, len2;
    
          public int compare(String s1, String s2)
          {
            str1 = s1;
            str2 = s2;
            len1 = str1.length();
            len2 = str2.length();
            pos1 = pos2 = 0;
    
            int result = 0;
            while (result == 0 && pos1 < len1 && pos2 < len2)
            {
              char ch1 = str1.charAt(pos1);
              char ch2 = str2.charAt(pos2);
    
              if (Character.isDigit(ch1))
              {
                result = Character.isDigit(ch2) ? compareNumbers() : -1;
              }
              else if (Character.isLetter(ch1))
              {
                result = Character.isLetter(ch2) ? compareOther(true) : 1;
              }
              else
              {
                result = Character.isDigit(ch2) ? 1
                       : Character.isLetter(ch2) ? -1
                       : compareOther(false);
              }
    
              pos1++;
              pos2++;
            }
    
            return result == 0 ? len1 - len2 : result;
          }
    
          private int compareNumbers()
          {
            int end1 = pos1 + 1;
            while (end1 < len1 && Character.isDigit(str1.charAt(end1)))
            {
              end1++;
            }
            int fullLen1 = end1 - pos1;
            while (pos1 < end1 && str1.charAt(pos1) == '0')
            {
              pos1++;
            }
    
            int end2 = pos2 + 1;
            while (end2 < len2 && Character.isDigit(str2.charAt(end2)))
            {
              end2++;
            }
            int fullLen2 = end2 - pos2;
            while (pos2 < end2 && str2.charAt(pos2) == '0')
            {
              pos2++;
            }
    
            int delta = (end1 - pos1) - (end2 - pos2);
            if (delta != 0)
            {
              return delta;
            }
    
            while (pos1 < end1 && pos2 < end2)
            {
              delta = str1.charAt(pos1++) - str2.charAt(pos2++);
              if (delta != 0)
              {
                return delta;
              }
            }
    
            pos1--;
            pos2--; 
    
            return fullLen2 - fullLen1;
          }
    
          private int compareOther(boolean isLetters)
          {
            char ch1 = str1.charAt(pos1);
            char ch2 = str2.charAt(pos2);
    
            if (ch1 == ch2)
            {
              return 0;
            }
    
            if (isLetters)
            {
              ch1 = Character.toUpperCase(ch1);
              ch2 = Character.toUpperCase(ch2);
              if (ch1 != ch2)
              {
                ch1 = Character.toLowerCase(ch1);
                ch2 = Character.toLowerCase(ch2);
              }
            }
    
            return ch1 - ch2;
          }
        }
    

    Now, use it like this:

      Arrays.sort(files, new WindowsExplorerStringComparator());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which consists of two simple Java Swing files. How do
I have avery simple program which uses MediaPLayer. I am new to java and
I have a simple program (written in Java) which uses the google protocol buffer
I have a simple java code that reads text csv file that contains sentences
I have a simple java program which is just a single piece of code
I have a program written in Java that reads in a file that is
I have Simple java program named MainController.java. Wehn I try to compile it from
i found many related questions here. I have a simple java program . it
I have been trying to call a mapreduce job from a simple java program
I made a simple java program that sends bytes to the parallel port, which

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.