Comparing strings by their alphabetical order

前端 未结 7 2091
灰色年华
灰色年华 2020-11-27 04:26
String s1 = \"Project\";
String s2 = \"Sunject\";

I want to compare the two above string by their alphabetic order (which in this case \"Project\"

相关标签:
7条回答
  • 2020-11-27 05:04

    As others suggested, you can use String.compareTo(String).

    But if you are sorting a list of Strings and you need a Comparator, you don't have to implement it, you can use Comparator.naturalOrder() or Comparator.reverseOrder().

    0 讨论(0)
提交回复
热议问题