Tuple Array Sorting

Vino vino.bheeman at hotmail.com
Mon Dec 11 16:15:14 UTC 2017


On Monday, 11 December 2017 at 15:54:11 UTC, Biotronic wrote:
> On Monday, 11 December 2017 at 15:33:08 UTC, Vino wrote:
>> On Monday, 11 December 2017 at 15:15:47 UTC, Biotronic wrote:
>>>   [...]
>>
>> Hi,
>>
>>  I tired that but no luck, below is the output, in your code 
>> you have one folder "auto folders =  ["D:\\Dev"];" if you have 
>> multiple folder then output is not sorted.
>>
>> C:\Temp\BACKUP\dir2                                     
>> 2017-Sep-09 22:44:11
>> C:\Temp\BACKUP\dir1                                     
>> 2017-Sep-06 16:06:42
>> C:\Temp\BACKUP\DND3                                     
>> 2017-Sep-05 14:31:00
>> C:\Temp\BACKUP\t1                                       
>> 2017-Dec-11 04:10:02
>> C:\Temp\BACKUP\dir3                                     
>> 2017-Dec-10 06:56:07
>> C:\Temp\EXPORT\DND6                                     
>> 2017-Sep-05 14:31:00
>> C:\Temp\PROD_TEAM\DND1                                  
>> 2017-Sep-05 14:31:01
>
> Are you sure that's the output from my code? Let's step through 
> the code:
>
>     // Iterating over folders:
>     folders
>     // Create a range where each element is a range of DirEntry
>     // in the given folder.
>     .map!(a => dirEntries(a, SpanMode.shallow))
>     // Join these together to a single range of DirEntry 
> instead of
>     // a range-of-ranges-of-DirEntry.
>     .join
>     // Remove anything that's not a folder.
>     .filter!(a => a.isDir)
>     // Grab the information we actually care about.
>     .map!(a => tuple(a.name, a.timeCreated))
>     // Enumerate to an array, so we can sort it.
>     .array
>     // Sort this array by the second tuple element 
> (timeCreated).
>     .sort!((a,b) => a[1] > b[1]);
>
> If this code does not do what you're asking, there's a bug 
> outside of the code, probably in the standard library.
>
> If instead you are invoking the program multiple times with a 
> single folder each time, the output you describe is to be 
> expected.
>
> Apart from that, I'm not sure what could be wrong.
>
> --
>   Biotronic

Hi Biotronic,

  I tried your code with multiple folder's , but no luck the 
output is not sorted.

Program:
import std.algorithm: filter, map, sort;
import std.array;
import std.file: SpanMode, dirEntries, isDir ;
import std.stdio: writefln, writeln;
import std.typecons: Tuple, tuple;

void main () {
auto FFs =  ["C:\\Temp\\sapnas2\\BACKUP", 
"C:\\Temp\\sapnas2\\EXPORT", "C:\\Temp\\sapnas2\\PROD_TEAM"];
auto sorted = FFs
.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);
}

From,
Vino.B



More information about the Digitalmars-d-learn mailing list