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.

C W
  • 1
  • 1
  • Where should, say, `1Foo.jpg` be in this arrangement? – Joe C Jan 12 '19 at 21:14
  • You are using string compare. You have to strip the Filename convert it to an intager and sort it – Jens Jan 12 '19 at 21:19
  • "Where should, say, 1Foo.jpg be in this arrangement?" good question. – C W Jan 12 '19 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. – C W Jan 12 '19 at 21:23
  • Is there a way to get the metadata in java so I could sort by date instead? – C W Jan 12 '19 at 21:24

2 Answers2

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!

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Do you know how to get at meta data for the file in java? Creation date, last modified, size etc? – C W Jan 12 '19 at 21:42
  • For dates look here: https://stackoverflow.com/questions/2723838/determine-file-creation-date-in-java, For file size: https://stackoverflow.com/questions/7226588/files-size-implementation-in-java-7 – Jens Jan 12 '19 at 21:44
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.

Rohit Shetty
  • 341
  • 2
  • 10