-2

I have two Strings:

String string1 = "Abcdef"
String string2 = "Axyz"

I usually code in python, so I'm really new to java. How do I compare these to strings and find out which one would come first in a dictionary(I mean the book not the datastructure)?

Sven
  • 790
  • 7
  • 22
  • 2
    String have a method that is literaly called `compareTo(String)` with the description `Compares two strings lexicographically.`. I'm sorry but this question shows no research effort at all. – OH GOD SPIDERS May 19 '20 at 09:17
  • [Comparing strings by their alphabetical order](https://stackoverflow.com/questions/6203411/comparing-strings-by-their-alphabetical-order) – Robby Cornelissen May 19 '20 at 09:18
  • It is more complicated, because lexicographical ordering doesn't necessarily equal alphabetical ordering. – Robby Cornelissen May 19 '20 at 09:26

1 Answers1

1

Since String implements Comparable you can use compareTo to compare chronologically:

string1.compareTo(string2)
Aniket Sahrawat
  • 11,710
  • 3
  • 36
  • 63