Natural sorting: Difference between revisions

m (→‎{{header|D}}: fix of method name)
Line 393:
Implements requests 1-5.
<lang d>import std.stdio, std.string, std.algorithm, std.array, std.conv,
std.ascii, std.range;
 
string[] naturalSort(string[] arr) /*pure @safe*/ {
Line 401:
int opCmp(in ref Part other) const pure {
return (s[0].isDigit && other.s[0].isDigit) ?
cmp([s.to!ulong], [other.s.to!ulong]) :
cmp(s, other.s);
}
}
Line 421:
 
void main() /*@safe*/ {
autoconst tests = [
// Ignoring leading spaces.
["ignore leading spaces: 2-2", " ignore leading spaces: 2-1", "
ignore leading spaces: 2+1", " ignore leading spaces: 2+0"],
 
// 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"],
 
// 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"],
 
// Case Indepenent [sic] sort.
["cASE INDEPENENT: 3-2" /* [sic] */, "caSE INDEPENENT: 3-1" /* [sic] */,
"casE INDEPENENT: 3+0" /* [sic] */, "case INDEPENENT: 3+1" /* [sic] */],
 
// Numeric fields as numerics.
["foo100bar99baz0.txt", "foo100bar10baz0.txt",
"foo1000bar99baz10.txt", "foo1000bar99baz9.txt"],
 
// Title sorts.
["The Wind in the Willows", "The 40th step more",
"The 39 steps", "Wanda"]];
 
void printTexts(Range)(string tag, Range range) {
foreach (test; tests)
const sic = range.front.canFind("INDEPENENT") ? " [sic]" : "";
writeln(test, "\n", test.naturalSort, "\n");
writefln("\n%s%s:\n%-( |%s|%|\n%)", tag, sic, range);
}</lang>
}
 
foreach (test; tests) {
printTexts("Test strings", test);
printTexts("Normally sorted", test.dup.sort());
printTexts("Naturally sorted", test.dup.naturalSort());
}
}
</lang>
{{out}}
<pre>Test strings:
<pre>["ignore leading spaces: 2-2", " ignore leading spaces: 2-1", " ignore leading spaces: 2+1", " ignore leading spaces: 2+0"]
[" ignore leading spaces: 2+0", " ignore leading spaces: 2+1", " ignore leading spaces: 2-1", "|ignore leading spaces: 2-2"]|
| ignore leading spaces: 2-1|
|
ignore leading spaces: 2+1|
| ignore leading spaces: 2+0|
 
Normally sorted:
|
ignore leading spaces: 2+1|
| ignore leading spaces: 2+0|
| ignore leading spaces: 2-1|
|ignore leading spaces: 2-2|
 
Naturally sorted:
| ignore leading spaces: 2+0|
|
ignore leading spaces: 2+1|
| ignore leading spaces: 2-1|
|ignore leading spaces: 2-2|
 
Test strings:
|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|
 
Normally sorted:
|ignore m.a.s spaces: 2+1|
|ignore m.a.s spaces: 2+0|
|ignore m.a.s spaces: 2-1|
|ignore m.a.s spaces: 2-2|
 
Naturally sorted:
|ignore m.a.s spaces: 2+0|
|ignore m.a.s spaces: 2+1|
|ignore m.a.s spaces: 2-1|
|ignore m.a.s spaces: 2-2|
 
Test strings:
|Equiv. spaces: 3-3|
spaces: 3-2|
|Equiv.�spaces: 3-1|
|Equiv.�spaces: 3+0|
|Equiv.
spaces: 3+1|
|Equiv. spaces: 3+2|
 
Normally sorted:
|Equiv. spaces: 3+2|
|Equiv.
spaces: 3+1|
|Equiv.�spaces: 3+0|
|Equiv.�spaces: 3-1|
spaces: 3-2|
|Equiv. spaces: 3-3|
 
Naturally sorted:
|Equiv.�spaces: 3+0|
|Equiv.
spaces: 3+1|
|Equiv. spaces: 3+2|
|Equiv.�spaces: 3-1|
spaces: 3-2|
|Equiv. spaces: 3-3|
 
Test strings [sic]:
|cASE INDEPENENT: 3-2|
|caSE INDEPENENT: 3-1|
|casE INDEPENENT: 3+0|
|case INDEPENENT: 3+1|
 
Normally sorted [sic]:
|cASE INDEPENENT: 3-2|
|caSE INDEPENENT: 3-1|
|casE INDEPENENT: 3+0|
|case INDEPENENT: 3+1|
 
Naturally sorted [sic]:
|casE INDEPENENT: 3+0|
|case INDEPENENT: 3+1|
|caSE INDEPENENT: 3-1|
|cASE INDEPENENT: 3-2|
 
Test strings:
|foo100bar99baz0.txt|
|foo100bar10baz0.txt|
|foo1000bar99baz10.txt|
|foo1000bar99baz9.txt|
 
Normally sorted:
["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"]
|foo1000bar99baz10.txt|
["ignore m.a.s spaces: 2+0", "ignore m.a.s spaces: 2+1", "ignore m.a.s spaces: 2-1", "ignore m.a.s spaces: 2-2"]
|foo1000bar99baz9.txt|
|foo100bar10baz0.txt|
|foo100bar99baz0.txt|
 
Naturally sorted:
["Equiv. spaces: 3-3", "Equiv.\rspaces: 3-2", "Equiv.\fspaces: 3-1", "Equiv.\vspaces: 3+0", "Equiv.\nspaces: 3+1", "Equiv.\tspaces: 3+2"]
|foo1000bar99baz10.txt|
["Equiv.\vspaces: 3+0", "Equiv.\nspaces: 3+1", "Equiv.\tspaces: 3+2", "Equiv.\fspaces: 3-1", "Equiv.\rspaces: 3-2", "Equiv. spaces: 3-3"]
|foo1000bar99baz9.txt|
|foo100bar10baz0.txt|
|foo100bar99baz0.txt|
 
Test strings:
["cASE INDEPENENT: 3-2", "caSE INDEPENENT: 3-1", "casE INDEPENENT: 3+0", "case INDEPENENT: 3+1"]
|The Wind in the Willows|
["casE INDEPENENT: 3+0", "case INDEPENENT: 3+1", "caSE INDEPENENT: 3-1", "cASE INDEPENENT: 3-2"]
|The 40th step more|
|The 39 steps|
|Wanda|
 
Normally sorted:
["foo100bar99baz0.txt", "foo100bar10baz0.txt", "foo1000bar99baz10.txt", "foo1000bar99baz9.txt"]
|The 39 steps|
["foo100bar10baz0.txt", "foo100bar99baz0.txt", "foo1000bar99baz9.txt", "foo1000bar99baz10.txt"]
|The 40th step more|
|The Wind in the Willows|
|Wanda|
 
Naturally sorted:
["The Wind in the Willows", "The 40th step more", "The 39 steps", "Wanda"]
|The 39 steps|
["The 39 steps", "The 40th step more", "The Wind in the Willows", "Wanda"]</pre>
|The 40th step more|
|The Wind in the Willows|
|Wanda|
</pre>
 
=={{header|Elixir}}==
Anonymous user