0
File [] templatesList = templateFileList.toArray(new File[0] );
Arrays.sort(templatesList,ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE);

templatesList contains files like

  • app.vm
  • app.1.ivm
  • app.2.ivm
  • mart.ivm
  • mart.vm

First reverse sort based on last extension, then sort based on basename, then sort numbers. After sorting, expected output is

  • app.vm
  • mart.vm
  • app.1.ivm
  • app.2.ivm
  • mart.ivm

My Code:

    File [] templatesList = templateFileList.toArray(new File[0] );
    List <File> tempList = Arrays.asList(templatesList);
    tempList.stream().sorted(ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE.
            thenComparing(NameFileComparator.NAME_INSENSITIVE_COMPARATOR));     

        List <File> templateFileList1 = new ArrayList(Arrays.asList(templatesList));
        Collections.sort(templateFileList1, new Comparator <File>() {

            @Override
            public int compare(File f1, File f2) {
                return Integer.parseInt(FilenameUtils.getExtension(FilenameUtils.getBaseName(f1.toString())))-
                        Integer.parseInt(FilenameUtils.getExtension(FilenameUtils.getBaseName(f2.toString())));
            }
        });
2
  • 2
    Have you tried something yet? Commented Apr 4, 2018 at 10:49
  • yes i tried. i added my code now. can you please check now Commented Apr 4, 2018 at 11:03

2 Answers 2

1

You could mix ExtensionFileComparator and NameFileComparator from Apache commons Comparators.
With Java 8 :

Arrays.sort(templatesList, ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE
              .thenComparing(NameFileComparator.NAME_INSENSITIVE_COMPARATOR));

Or write it without any API with a little more boiler plate code.

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

Comments

0

if someone looking for answer

File [] templatesList = templateFileList.toArray(new File[0] );
    Arrays.sort(templatesList, ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE
            .thenComparing(NameFileComparator.NAME_INSENSITIVE_COMPARATOR).thenComparing(new Comparator <File>() {
                @Override
                public int compare(File f1, File f2) {
                    String file1 = FilenameUtils.getBaseName(f1.toString());
                    String file2 = FilenameUtils.getBaseName(f2.toString());

                    String ext1 = FilenameUtils.getExtension(file1);
                    String ext2 = FilenameUtils.getExtension(file2);

                    if ((StringUtils.isNumericSpace(ext1)) &&(StringUtils.isNumericSpace(ext2))) {
                        int t1= Integer.parseInt(ext1);
                        int t2= Integer.parseInt(ext2);
                        return t1-t2;
                    }
                    else {
                    return 0;
                    }
                }                   
            }));

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.