-2

I'm trying to sort some data. The data is a bunch of first and second names. We are sorting the second names by alphabetical order and then if the second names are the same we sort the first names by alphabetical order. When I do the following:

string a = ABC
string b = aab
string c = abcd

a.compareTo(b) = -32
c.compareTo(b)=1

Using this method the order returned from my implemented merge sort would be:

ABC
aab
abcd

as it would result in adding the order of strings from lowest to highest to my array. How would I go about possibly converting the capitals or implementing additional checks to order it like:

aab
ABC
abcd
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#CASE_INSENSITIVE_ORDER – boneill Mar 13 '22 at 19:50

1 Answers1

0

Just use a.compareToIgnoreCase(b)

Documentation link

passer-by
  • 1,103
  • 2
  • 4