Java Sorting a list, ignoring apostrophe
I have a list that I am sorting that reads from a CSV file.
Now when I sort the code I get:
But if I sort the CSV file in Excel it is slightly different
So, technically Excel ignores the Apostrophe case, but mine does not. The criteria do not indicate what to do in this case, but I would assume that my code is incorrect. How would I ignore the Apostrophe case and move on to the next character?
My code:
public static Comparator<Hill> compareName = new Comparator<Hill>() {
public int compare(Hill one, Hill other) {
return one.name.compareTo(other.name);
}
};
public static void exercise5d() {
List<Hill> hills = readHills();
for (int i = 0; i < 20; i++) {
Collections.sort(hills, Hill.compareName);
System.out.println(hills.get(i));
}
}
I guess there Hill.name
is String
.
In your comparator, you can compare String
after replacing the apostrophes like below:
public int compare(Hill one, Hill other) {
return one.name.replaceAll("'", "").compareTo(other.name.replaceAll("'","");
}
But do you think you don't want to be considered '
?
source to share