Filenamecomparator
Filenamecomparator
If you sort this array using the type an ASCII-value based comparator as described
above, the result is the following:
[“test1.txt”, “test10.txt”, “test2.txt”]
However, if you were to create 3 files with these names in the same directory on
your computer and viewed the contents of the directory sorted by name using a file
manager application (such as the Windows Explorer or the Finder), you will notice
that they are actually sorted as follows:
[“test1.txt”, “test2.txt”, “test10.txt”]
Your task is to write a method “compare” that compares two strings and orders
them in an intuitive manner. The prototype for the method in Java would be the
following:
int compare (String s1, String s2)
At minimum, the comparator you write must compare whole numbers in the string
using their natural ordering. A whole number is any sequence of numeric characters
(0-9) that occur consecutively. So, "1 2 10" would come before "1 10 2" (each of
these strings has 3 whole numbers and 2 is less than 10), but "1210" would come
after "1102" (each of these strings has 1 whole number and 1102 is less than 1210).
For the purposes of this problem, we will not consider any fractional numbers, so '.'
and '/' should just be treated as delimiters like any other character. That is to say,
"1/5" would come before "1/20" (each of these strings has 2 whole numbers that
are compared and 5 is less than 20). We will also not consider negative numbers,
meaning that ‘-‘ should be interpreted as a delimiter like any other character (“-1”
would come before “-5” because ‘-‘ is equal in both strings and 1 is less than 5).
Note that, while comparing numeric values in the string must be implemented as
outlined above, this is only the minimum requirement. The goal of this problem is to
write a comparator that enforces a “natural” sort ordering that is intuitive to end-
users. You should try to think of other things besides the ordering of numbers
within a string that may result in a sort order that would be unintuitive to end users.
This requirement is purposefully ambiguous — trying to figure out what cases may
be “unintuitive” and justifying your assertions is part of the problem.
Requirements:
Notes:
- You may assume that each input string will be no longer than 255 characters
- You may assume that all of the characters in the input strings will be 1-byte
ASCII printable characters in the range of 32 and 126 inclusive (that is, you
do not have to deal with Unicode, special character encodings or ASCII
control characters)
Evaluation:
The assignment should only require 4 hours of total work, but for convenience to
you and your interviewer, you will be allowed 48. When the 48 hours are up, or you
are finished, please email us your solution. We recommend including a paragraph
or two describing how you would continue to improve your solution if you had
more time.