Library contents

Groovy String.compareToIgnoreCase

Order text lexicographically after applying Java’s locale-independent case rules.

A simple example

String.compareToIgnoreCase

  1. Start with "a" and "A"
  2. Do this Compare ignoring case
  3. Result Equal
After case is ignored the two ASCII letters represent the same text.

Syntax

Groovy 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.groovy

Order 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.

Resulttrue
Lesson.groovy
println ("art".compareToIgnoreCase("BIO") < 0)
Output
true

Case is ignored when ordering these ASCII codes.

Advertisements

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.

Result0
Lesson.groovy
println "paid".compareToIgnoreCase("PAID")
Output
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?

Given'AR' · 'AT'
Worked resultAR

What to watch for

For names in a particular language, choose a Collator for that locale.

Documentation for String.compareToIgnoreCase