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: enter image description here

But if I sort the CSV file in Excel it is slightly different enter image description here

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));
    }

}

      

+3


source to share


2 answers


In the comparator

replace

return one.name.compareTo(other.name);



from

return one.name.replaceAll("'","").compareTo(other.name.replaceAll("'",""));

+3


source


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 '

?

0


source







All Articles