RosettaCodeData/Task/Natural-sorting/D/natural-sorting.d

67 lines
2.2 KiB
D
Raw Permalink Normal View History

2015-02-20 09:02:09 -05:00
import std.stdio, std.string, std.algorithm, std.array, std.conv,
2017-09-23 10:01:46 +02:00
std.ascii, std.range;
2015-02-20 09:02:09 -05:00
string[] naturalSort(string[] arr) /*pure @safe*/ {
static struct Part {
string s;
int opCmp(in ref Part other) const pure {
return (s[0].isDigit && other.s[0].isDigit) ?
2017-09-23 10:01:46 +02:00
cmp([s.to!ulong], [other.s.to!ulong]) :
cmp(s, other.s);
2015-02-20 09:02:09 -05:00
}
}
static mapper(in string txt) /*pure nothrow @safe*/ {
auto r = txt
.strip
.tr(whitespace, " ", "s")
.toLower
2017-09-23 10:01:46 +02:00
.chunkBy!isDigit
2015-02-20 09:02:09 -05:00
.map!(p => Part(p.text))
.array;
return (r.length > 1 && r[0].s == "the") ? r.dropOne : r;
}
return arr.schwartzSort!mapper.release;
}
void main() /*@safe*/ {
2017-09-23 10:01:46 +02:00
const tests = [
// Ignoring leading spaces.
["ignore leading spaces: 2-2", " ignore leading spaces: 2-1", "
2015-02-20 09:02:09 -05:00
ignore leading spaces: 2+1", " ignore leading spaces: 2+0"],
2017-09-23 10:01:46 +02:00
// Ignoring multiple adjacent spaces (m.a.s).
["ignore m.a.s spaces: 2-2", "ignore m.a.s spaces: 2-1",
"ignore m.a.s spaces: 2+0", "ignore m.a.s spaces: 2+1"],
2015-02-20 09:02:09 -05:00
2017-09-23 10:01:46 +02:00
// Equivalent whitespace characters.
["Equiv. spaces: 3-3", "Equiv.\rspaces: 3-2",
"Equiv.\x0cspaces: 3-1", "Equiv.\x0bspaces: 3+0",
"Equiv.\nspaces: 3+1", "Equiv.\tspaces: 3+2"],
2015-02-20 09:02:09 -05:00
2017-09-23 10:01:46 +02:00
// Case Indepenent [sic] sort.
["cASE INDEPENENT: 3-2" /* [sic] */, "caSE INDEPENENT: 3-1" /* [sic] */,
"casE INDEPENENT: 3+0" /* [sic] */, "case INDEPENENT: 3+1" /* [sic] */],
2015-02-20 09:02:09 -05:00
2017-09-23 10:01:46 +02:00
// Numeric fields as numerics.
["foo100bar99baz0.txt", "foo100bar10baz0.txt",
"foo1000bar99baz10.txt", "foo1000bar99baz9.txt"],
2015-02-20 09:02:09 -05:00
2017-09-23 10:01:46 +02:00
// Title sorts.
["The Wind in the Willows", "The 40th step more",
"The 39 steps", "Wanda"]];
2015-02-20 09:02:09 -05:00
2017-09-23 10:01:46 +02:00
void printTexts(Range)(string tag, Range range) {
const sic = range.front.canFind("INDEPENENT") ? " [sic]" : "";
writefln("\n%s%s:\n%-( |%s|%|\n%)", tag, sic, range);
}
foreach (test; tests) {
printTexts("Test strings", test);
printTexts("Normally sorted", test.dup.sort());
printTexts("Naturally sorted", test.dup.naturalSort());
}
2015-02-20 09:02:09 -05:00
}