Natural sorting
class String {
# Sort groups of digits in number order. Sort by order of magnitude then lexically.
-> naturally { self.lc.gsub(/(\d+)/, {|s1| "0" + s1.len.chr + s1 }) + "\x0" + self };
# Collapse multiple ws characters to a single.
-> collapse { self.gsub(/(\s)\1+/, {|s1| s1 }) };
# Convert all ws characters to a space.
-> normalize { self.gsub(/(\s)/, ' ') };
# Ignore common leading articles for title sorts
-> title { self.sub(/^(?:a|an|the)\b\s*/i, '') };
# Decompose ISO-Latin1 glyphs to their base character.
-> latin1_decompose {
static tr = Hash.new(%w(
Æ AE æ ae Þ TH þ th Ð TH ð th ß ss À A Á A Â A Ã A Ä A Å A à a á a
â a ã a ä a å a Ç C ç c È E É E Ê E Ë E è e é e ê e ë e Ì I Í I Î
I Ï I ì i í i î i ï i Ò O Ó O Ô O Õ O Ö O Ø O ò o ó o ô o õ o ö o
ø o Ñ N ñ n Ù U Ú U Û U Ü U ù u ú u û u ü u Ý Y ÿ y ý y
)...);
var re = Regex.new('(' + tr.keys.join('|') + ')');
self.gsub(re, {|s1| tr{s1} });
}
}Tests:
Output:
Last updated
Was this helpful?