Tuple Array Sorting

Biotronic simen.kjaras at gmail.com
Mon Dec 11 15:15:47 UTC 2017


On Monday, 11 December 2017 at 14:52:35 UTC, Vino wrote:
> Example Program and Output
>
> import std.algorithm: filter, map, sort;
> import std.container.array;
> import std.file: SpanMode, dirEntries, isDir ;
> import std.range: chain;
> import std.stdio: writefln;
> import std.typecons: Tuple, tuple;
>
> void main () {
> auto FFs =  Array!(string)("C:\\Temp\\BACKUP", 
> "C:\\Temp\\EXPORT", "C:\\Temp\\PROD_TEAM");
> int AgeSize = 2;
> foreach(d; FFs[]) {
> auto dFiles = Array!(Tuple!(string, string))(dirEntries(d, 
> SpanMode.shallow).filter!(a => a.isDir).map!(a => tuple(a.name, 
> a.timeCreated.toSimpleString[0 .. 20])));
> writefln("%(%-(%-63s %s %)\n%)", chain(dFiles[]).sort!((a,b) => 
> a[0] > b[0]));
> }
> }

You're somewhat close. You're sorting based on the 0th element of 
your tuples, while you should sort on the 1st. Something like 
this:

import std.algorithm: filter, map, sort;
import std.array : array;
import std.file: SpanMode, dirEntries, isDir ;
import std.range: join;
import std.stdio: writefln;
import std.typecons: tuple;

void main () {
     auto folders =  ["D:\\Dev"];

     auto sorted = folders
         .map!(a => dirEntries(a, SpanMode.shallow))
         .join
         .filter!(a => a.isDir)
         .map!(a => tuple(a.name, a.timeCreated.toSimpleString[0 
.. 20]))
         .array
         .sort!((a,b) => a[1] > b[1]);

     writefln("%( %( %-63s %s %) \n%)", sorted);
}

--
   Biotronic


More information about the Digitalmars-d-learn mailing list