2 Dimensional Array Sorting
Vino.B via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Aug 26 02:53:44 PDT 2017
On Saturday, 26 August 2017 at 06:12:57 UTC, user1234 wrote:
> On Saturday, 26 August 2017 at 06:11:37 UTC, user1234 wrote:
>> On Saturday, 26 August 2017 at 06:01:15 UTC, Vino.B wrote:
>>> Hi,
>>>
>>> Can someone provide me a example of sorting 2 Dimensional
>>> Array containing Filename and Size, and should be sorted by
>>> Size.
>>>
>>> From,
>>> Vino.B
>>
>> void main(string[] args)
>> {
>> import std.stdio;
>> import std.algorithm.sorting;
>>
>> string[2][] nameAndSize = [["b", "2"], ["a", "1"]];
>> auto s = nameAndSize.sort!((a,b) => a[0] < b[0] && a[1] <
>> b[1]);
>> writeln(s);
>> }
>
> I missed the "by Size" directive. So it's just:
>
> void main(string[] args)
> {
> import std.stdio;
> import std.algorithm.sorting;
>
> string[2][] nameAndSize = [["b", "2"], ["a", "1"]];
> auto s = nameAndSize.sort!((a,b) => a[1] < b[1]);
> writeln(s);
> }
Hi,
I tired you logic, but doesn't seem to be working, as every
time i execute the order of the file list is different as in the
below program i have used the sort based on the file name.
Program:
import std.file: dirEntries, isFile, SpanMode;
import std.stdio: writeln, writefln;
import std.algorithm: filter, map, sort;
import std.path: globMatch, baseName;
import std.array: array, replace;
import std.typecons: tuple;
import std.parallelism;
import std.conv;
int SizeDir = 10;
string[][] Subdata;
auto Dirlst = [ "C:\\Temp\\TEAM2\\TEAM",
"C:\\Temp\\TEAM2\\PROD_TEAM", "C:\\Temp\\TEAM3\\BACKUP",
"C:\\Temp\\TEAM3\\EXPORT"];
string[][] SizeDirList (string Fs)
{
ulong subdirTotal = 0;
auto unc = "\\\\?\\"~Fs;
auto dFiles = dirEntries(unc, SpanMode.shallow).filter!(a =>
a.isDir && !globMatch(a.baseName, "*DND*")).map!(a =>
tuple(a.name, a.size)).array;
foreach (d; dFiles)
{
auto SdFiles = dirEntries(d[0], SpanMode.depth).map!(a =>
tuple(a.size)).array;
foreach (f; parallel(SdFiles,1))
{ subdirTotal += f[0]; }
ulong subdirTotalGB = (subdirTotal/1024);
if (subdirTotalGB > SizeDir)
{ Subdata ~= [d[0].replace("\\\\?\\", ""),
to!string(subdirTotalGB)]; }
subdirTotal = 0;
}
return Subdata;
}
void main ()
{
foreach (string Fs; parallel(Dirlst[0 .. $],1))
{
auto SizeDirListTask = task(&SizeDirList, Fs);
SizeDirListTask.executeInNewThread();
auto SizeDirListTaskData = SizeDirListTask.yieldForce;
auto sortedresult = SizeDirListTaskData.sort!((a,b) => a[0] <
b[0]);
foreach(i; sortedresult[0 .. $])
writefln("%-(%-63s %)", i);
}
}
Output :The sequence is not correct and there are duplicates.
C:\Users\admin\Desktop\Script\D>rdmd Ftest.d
C:\Temp\TEAM2\TEAM\DIR1 40
C:\Temp\TEAM2\TEAM\DIR2 2228
C:\Temp\TEAM2\TEAM\DIR2 2228
C:\Temp\TEAM2\PROD_TEAM\dir1
35772
C:\Temp\TEAM2\TEAM\DIR2 2228
C:\Temp\TEAM2\PROD_TEAM\dir1
35772
C:\Temp\TEAM3\BACKUP\dir1
71465
C:\Temp\TEAM3\EXPORT\dir2 61
C:\Users\admin\Desktop\Script\D>rdmd Ftest.d
C:\Temp\TEAM2\TEAM\DIR1 40
C:\Temp\TEAM2\TEAM\DIR2 2228
C:\Temp\TEAM2\PROD_TEAM\dir1
35772
C:\Temp\TEAM3\BACKUP\dir1
71465
C:\Temp\TEAM3\EXPORT\dir2 61
C:\Users\admin\Desktop\Script\D>rdmd Ftest.d
C:\Temp\TEAM2\TEAM\DIR1 40
C:\Temp\TEAM2\TEAM\DIR2 2228
C:\Temp\TEAM3\BACKUP\dir1
71465
C:\Temp\TEAM3\EXPORT\dir2 61
C:\Temp\TEAM2\PROD_TEAM\dir1
35772
Required Output :C:\Temp\TEAM2\..., C:\Temp\TEAM3...
C:\Temp\TEAM2\TEAM\DIR1 40
C:\Temp\TEAM2\TEAM\DIR2 2228
C:\Temp\TEAM2\PROD_TEAM\dir1
35772
C:\Temp\TEAM3\BACKUP\dir1
71465
C:\Temp\TEAM3\EXPORT\dir2 61
From,
Vino.B
More information about the Digitalmars-d-learn
mailing list