If I had one string that was String a = "Apple" and another that was String "b" = "Banana" how would I be able to compare the two and get something like (b > a) evaluates to true in Clojure?
2 Answers
The built-in Clojure function compare
is a 3-way comparator for many kinds of values, including strings, but it compares characters by their UTF-16 Unicode code points, which for the ASCII subset will compare upper-case letters differently than lower-case letters, so maybe not what you are looking for.
If you want case-insensitive comparison between strings with all characters in the ASCII subset, you could use something like this:
(require '[clojure.string :as str])
(compare (str/lower-case s1) (str/lower-case s2))
If you want fancier locale-dependent string comparison for different languages/locale settings, there are Java libraries for that, which you can call via Java interop, e.g. see https://docs.oracle.com/javase/tutorial/i18n/text/locale.html or the ICU4J library: http://site.icu-project.org/home/why-use-icu4j
I do not know of any recommended way to replace the definitions of clojure.core/<
and clojure.core/>
so that they behave differently than they do for strings. You can defn
them in your own code and override the definitions in clojure.core
, but you will likely get warnings about doing so from the Clojure compiler. If you don't mind the warnings, go for it, but you'd better warn anyone else co-developing or using your code that you are doing so, to avoid misunderstandings.
You can define you own functions named <
>
etc. in your own namespace to behave however you like, of course.

- 1,486
- 8
- 10
You can find string comparison functions here, along with many other helper & convenience functions.
(increasing? a b)
Returns true if a pair of strings are in increasing lexicographic order.
(increasing-or-equal? a b)
Returns true if a pair of strings are in increasing lexicographic order, or equal.

- 29,276
- 6
- 41
- 48