Forums Register Login

Comparing two-character strings alphabetically.

+Pie Number of slices to send: Send
Disclaimer: I'm not sure if this belongs in Intermediate or Beginner, but I'm giving it a shot here. Please don't hurt me.

What I'm trying to accomplish is this:

I have to read in this field from a database, which is a two-character field that starts at "ZZ", and decrements "ZZ", "ZY", "ZX", "ZW", etc. I have to grab the last two ("ZX", and "ZW", for example) for any given group of records, and then use those codes to do further queries. (Think of them as representing days, and I'm needing to get info for the last two days.) So, to recap, out of a bunch of these ZZ - ?? codes, I need to find and use the "lowest" two.

I've managed to get it to work, more or less, by breaking the string apart using .charAt() and then using Character.valueOf(), and comparing the unicode, but it's ugly and I'm fairly certain that there has to be a better way that I'm overlooking.

Any suggestions?

Thanks in advance.
+Pie Number of slices to send: Send
Well, here is one solution: put the candidate Strings in the list, and sort it using the well defined Collections API. After the sorting, the last two Strings in the list will be what you want:


Output: [AA, AZ, BB, CC, ZY, ZZ]
[ June 09, 2005: Message edited by: John Smith ]
+Pie Number of slices to send: Send
Can your database query just order by this field?

Failing that, look at the compareTo() method. The List solution may well be the best today, but compareTo() is worth knowing, too.
It's a tiny ad only because the water is so cold.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1920 times.
Similar Threads
hashCode() and System.IdentityHashCode
Is there any way to get the actual URL
Closing connection and resultset?
Weird Encoding
Same Form, More than Once
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:16:24.