0

How a list of filenames with numeric characters be sorted? Should the file part and numeric part be split. Below is the question but the filename could also be file1xx22yy.gif and so on.

Sort a list of file by names, that has numbers in them. Sort a list of files like: "file1" "file10" "file2" in the correct order: String length -> Number precedence

So actual list needs to be "file1" "file2" "file10"

I tried sorting using string sort but the results did not match the output specified in the question

Ricky
  • 1
  • 2
    You were given a clue in the instructions. Shorter strings sort before longer strings. Write a [Comparator](https://stackoverflow.com/questions/2839137/how-to-use-comparator-in-java-to-sort) that takes the string length into account. – Gilbert Le Blanc Feb 12 '23 at 05:30
  • Thank you. The file name could also be file01. Would the Comparator approach with size still work? – Ricky Feb 12 '23 at 05:37
  • In your compaator skip any common prefix (for example `File`). If at the end of both file names, they are equal (return 0). If at the end of only one of them, that one comes first. If not at the end of any, look at the current character of each file name. If *both* are numeric, read a number from each file name and compare the numbers. Else just compare the characters. Then you are done. – Ole V.V. Feb 12 '23 at 05:45

0 Answers0