Groovy String.compareToIgnoreCase
Order text lexicographically after applying Java’s locale-independent case rules.
A simple example
String.compareToIgnoreCase
- Start with "a" and "A"
- Do this Compare ignoring case
- Result Equal
Syntax
public int compareToIgnoreCase(String str)
Run these examples in Groovy
Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.
groovy Lesson.groovyOrder room prefixes
A directory orders short labels entered using different capitalization. Decide when two entries should compare as a tie and when the comparison should place one label first.
println ("art".compareToIgnoreCase("BIO") < 0)
true
Case is ignored when ordering these ASCII codes.
Compare delivery statuses
A dispatch board receives status text from two suppliers. Compare the labels using its chosen case convention and decide whether either status should sort ahead of the other.
println "paid".compareToIgnoreCase("PAID")
0
Zero indicates a tie under this comparison.
Sort art studio lockers
A school uses two-letter locker prefixes. The catalog compares the two ASCII codes in text order. Which code sorts first?
What to watch for
For names in a particular language, choose a Collator for that locale.