1

My problem comes down to this: I need to sort files in a specific order (the files got numbers at the beginning). Later I want to store them externally, the files are then sorted by alphabet by the system they're on, I got no influence on that process. So how could I rename them to make them stay in the correct order?

// this comparator sorts them by alphabetical order 
    Comparator<Mp3File> compAlphabetical = (x,y) -> getMp3TitleFromFilename(x).compareTo(getMp3TitleFromFilename(y));

//and this one does by number
//the inputs look like this "582 Some File Name" so they have to be edited with some regex before using them for sorting
Comparator<Mp3File> compNumeric = new Comparator<Mp3File>() {

            @Override
            public int compare(Mp3File o1, Mp3File o2) {
                Integer i1 = Integer.parseInt(getMp3TitleFromFilename(o1).substring(0,3).replaceAll("[^0-9]", ""));
                Integer i2 = Integer.parseInt(getMp3TitleFromFilename(o2).substring(0,3).replaceAll("[^0-9]", ""));
                return i1.compareTo(i2);
            }
        };

What I want to achieve is a method which gets the list with the correct sorting (2nd) comparator and renames the files so they would maintain their order, even if I would run the first Comparator on the list.

Right now the sorting by alphabet puts out partly correct orders. It looks like this:

  • 1 One File Name
  • 10 Another File Name
  • 100 A Good File Name
  • 101 An Even Better File Name
  • 102 Another File
  • 103 A really good File Name

But this isn't really what I want so I thought about putting some letters at the beginning like this:

  • AAA One File
  • AAB Another File
  • AAC And
  • AAD So
  • AAE On

But I can't figure out how to properly convert those numbers to chars and how to make that working inside Java. Maybe one of you got an idea for me how to figure this out? Thanks in advance!

m1212e
  • 303
  • 4
  • 8
  • I have difficulty understanding why **string** file names starting with numbers poses a problem for sorting those names. Numerical values are low enough within the food chain (so to speak) that they should sort out properly with either Arrays.sort() or Collections.sort() methods. When numerical values are within a String they are not of Integer type, they are of String type. Therefore your first comparator should work fine whether the list contains names starting with digits or not. Unless of course you simply don't want the names to start with numbers. This of course can be achieved. – DevilsHnd - 退職した Sep 16 '19 at 19:44

1 Answers1

0

So your code is currently ordering the strings in lexicographical order, which basically means the language treats the variables as strings and orders them as such, comparing each character at each position of a string with another strings corresponding character and its corresponding position(e.g. "2" is greater than "1999999" because '2' is greater than '1').

You've probably seen this problem, and its solution, if you've ever looked at a folder containing episodes of a show(e.g. S3E08). You'll notice they prepend a '0' to the episode number so that the lexicographical sort doesn't mess up what we would expect to be the correct alphabetical order.

This is what I suggest that you do, I'll put an example of what the files names would look like below:

  • 001 One File Name
  • 010 Another File Name
  • 100 A Good File Name
  • 101 An Even Better File Name
  • 102 Another File
  • 103 A really good File Name

The algorithm to do this is fairly simple, so I'll leave that up to you! Feel free to post back if you have any questions on implementation

ShaneDemskie
  • 389
  • 2
  • 14