Natural sorted list of files

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 7 23:41:29 PST 2017


On 02/06/2017 09:00 PM, Dmitry wrote:
 > On Monday, 6 February 2017 at 18:57:17 UTC, Ali Çehreli wrote:
 >> I think  it's now std.algorithm.chunkBy. Please fix Rosetta
 >
 > Thank you!
 > I fixed

Thank you!

 > but anyway it works incorrect (it doesn't any changes):

The problem was with the following loop:

     foreach (test; tests)
         writeln(test, "\n", test.naturalSort, "\n");

test.naturalSort would sort the array in place before calling writeln 
and 'test' would appear naturally sorted as well. I've fixed it like this:

     foreach (test; tests) {
         printTexts("Test strings", test);
         printTexts("Normally sorted", test.dup.sort());
         printTexts("Naturally sorted", test.dup.naturalSort());
     }

I had fun inserting "[sic]"s both in source code and in the output for 
the INDEPENENT typo in the problem description. :D

     void printTexts(Range)(string tag, Range range) {
         const sic = range.front.canFind("INDEPENENT") ? " [sic]" : "";
         writefln("\n%s%s:\n%-(  |%s|%|\n%)", tag, sic, range);
     }

I also made the output more readable:

Test strings:
   |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|

[...]

Ali



More information about the Digitalmars-d-learn mailing list