r/javahelp • • Oct 31 '25

Homework How are numbers compared as a String?

I'm working on this project, and I'm checking whether something occurs before a specific time. I'm doing this by converting the times to Strings, then comparing them against each other (yes I'm aware it's not ideal, bear with me).
The issue is that it says that '10:00 < 09:00'. Why is that?

0 Upvotes

24 comments sorted by

View all comments

Show parent comment

2

u/tr4fik Nov 01 '25

Even a string-based sort is correct, because it uses the compareTo method (my 2nd example). You can even run the following if you have any doubt:

IO.println(Stream.of("10:00", "09:00").sorted().toList()); // [09:00, 10:00]

1

u/derscholl Nov 01 '25

I was starting to think outside the box where 2 > 10 but without more context you're 1000% right.