0

Is there a better way to sort this, to get the correct order? Thanks

import java.io.*;
import java.util.*;


public class JpgDirToHtm
{
    public static void main(String[] args) throws FileNotFoundException
    {




        Scanner kb = new Scanner(System.in);  
        System.out.print("Enter the path of the folder whose contents you wish to insert into an html file: ");
        String path = kb.nextLine(); 



        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        ArrayList<String> htmlTextList = new ArrayList<String>();

        for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                htmlTextList.add(listOfFiles[i].getName() );
            }


        }

        Collections.sort(htmlTextList);

        System.out.println(htmlTextList);
   }
}

This is what it prints [1.jpeg, 10.jpg, 11.jpeg, 12.jpeg, 13.jpeg, 14.jpeg, 16.jpg, 17.jpg, 18.jpg, 19.jpg, 2.jpeg, 20.jpg, 21.jpg, 22.jpg, 23.jpg, 24.jpg, 25.jpg, 3.jpg, 5.jpg, 7.jpeg, 9.jpg]

I need 2.jpeg to come after 1.jpeg et cetera.

Sorry, there is probably a simple fix but I haven't found anything on google. I am new to programming.

Everything else works really well. The whole program can take thousands of photos and automatically place them, sized correctly, in html web pages at a given number of photos per page that you can set. If any one is interested in having the rest of the code I will post it.

5
  • Where should, say, 1Foo.jpg be in this arrangement? Commented Jan 12, 2019 at 21:14
  • You are using string compare. You have to strip the Filename convert it to an intager and sort it Commented Jan 12, 2019 at 21:19
  • "Where should, say, 1Foo.jpg be in this arrangement?" good question. Commented Jan 12, 2019 at 21:22
  • "You are using string compare. You have to strip the Filename convert it to an intager and sort it" That is what I was afraid of. Commented Jan 12, 2019 at 21:23
  • Is there a way to get the metadata in java so I could sort by date instead? Commented Jan 12, 2019 at 21:24

2 Answers 2

1

Write your own comparator:

    Collections.sort(list, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            String filename1 =o1.substring(0,o1.indexOf("."));
            String filename2 =o2.substring(0,o2.indexOf("."));
            return Integer.valueOf(filename1).compareTo(Integer.valueOf(filename2));
        }
    });

This will convert the filename to an integer and compare it. But take care, it only works if your filenames are numbers!

Sign up to request clarification or add additional context in comments.

2 Comments

Do you know how to get at meta data for the file in java? Creation date, last modified, size etc?
0

You would have to write your own comparator and take care of the scenario of the flie starting with a number or a string:

public class JpgDirToHtm
{

    public static void main(String[] args) throws FileNotFoundException
    {
        Scanner kb = new Scanner(System.in);  
        System.out.print("Enter the path of the folder whose contents you wish to insert into an html file: ");
        String path = kb.nextLine(); 

        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        ArrayList<String> htmlTextList = new ArrayList<String>();

        for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                htmlTextList.add(listOfFiles[i].getName() );
            }


        }

        Collections.sort(htmlTextList, new Sortbyname());

        System.out.println(htmlTextList);
    }
}

class Sortbyname implements Comparator<String> 
{ 
    // Used for sorting in ascending order of 
    // roll name 
    public int compare(String a, String b) 
    { 
        String tempA = a.split("\\.")[0];
        String tempB = b.split("\\.")[0];

        try
        {
            return Integer.parseInt(tempA)-Integer.parseInt(tempB);
        }
        catch (Exception e)
        {
            return a.compareTo(b); 
        }
    } 
} 

The code catches any exceptions with formatting the number and just falls back to String compare.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.