# Comparator that efficienlty use `to_s` to compare things
#
# The comparaison call `to_s` on object and use the result to order things.
#
# ~~~
# var a = [1, 2, 3, 10, 20]
# (new CachedAlphaComparator).sort(a)
# assert a == [1, 10, 2, 20, 3]
# ~~~
#
# Internally the result of `to_s` is cached in a HashMap to counter
# uneficient implementation of `to_s`.
#
# Note: it caching is not usefull, see `alpha_comparator`
class CachedAlphaComparator
super Comparator
redef type COMPARED: Object
private var cache = new HashMap[Object, String]
private fun do_to_s(a: Object): String do
if cache.has_key(a) then return cache[a]
var res = a.to_s
cache[a] = res
return res
end
redef fun compare(a, b) do
return do_to_s(a) <=> do_to_s(b)
end
end
lib/core/text/abstract_text.nit:2464,1--2494,3