www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 2 Dimensional Array Sorting

reply Vino.B <vino.bheeman hotmail.com> writes:
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
Aug 25 2017
parent reply user1234 <user1234 12.hu> writes:
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); }
Aug 25 2017
parent reply user1234 <user1234 12.hu> writes:
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); }
Aug 25 2017
parent reply Vino.B <vino.bheeman hotmail.com> writes:
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
Aug 26 2017
parent reply user1234 <user1234 12.hu> writes:
On Saturday, 26 August 2017 at 09:53:44 UTC, Vino.B wrote:
 On Saturday, 26 August 2017 at 06:12:57 UTC, user1234 wrote:
 [...]
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. [...]
Try with (a,b) => a[1].to!int < b[1].to!int as predicate. T
Aug 26 2017
parent reply Vino.B <vino.bheeman hotmail.com> writes:
On Saturday, 26 August 2017 at 10:07:53 UTC, user1234 wrote:
 On Saturday, 26 August 2017 at 09:53:44 UTC, Vino.B wrote:
 On Saturday, 26 August 2017 at 06:12:57 UTC, user1234 wrote:
 [...]
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. [...]
Try with (a,b) => a[1].to!int < b[1].to!int as predicate. T
Hi, Now there is no duplicate , but the sequence is still not correct Output: the File C:\Temp\TEAM2\PROD_TEAM\dir1 is after C:\Temp\TEAM3\EXPORT\dir2 C:\Users\admin\Desktop\Script\D>rdmd Ftest.d C:\Temp\TEAM2\BACKUP\dir1 35732 C:\Temp\TEAM2\TEAM\DIR1 40 C:\Temp\TEAM2\TEAM\DIR2 2228 C:\Temp\TEAM2\EXPORT\dir2 30 C:\Temp\TEAM3\BACKUP\dir1 71465 C:\Temp\TEAM3\EXPORT\dir2 61 C:\Temp\TEAM2\PROD_TEAM\dir1 35772 Required output C:\Temp\TEAM2\BACKUP\dir1 35732 C:\Temp\TEAM2\TEAM\DIR1 40 C:\Temp\TEAM2\TEAM\DIR2 2228 C:\Temp\TEAM2\EXPORT\dir2 30 C:\Temp\TEAM2\PROD_TEAM\dir1 35772 C:\Temp\TEAM3\BACKUP\dir1 71465 C:\Temp\TEAM3\EXPORT\dir2 61 From, Vino.B
Aug 26 2017
parent reply Vino.B <vino.bheeman hotmail.com> writes:
On Saturday, 26 August 2017 at 10:45:13 UTC, Vino.B wrote:
 On Saturday, 26 August 2017 at 10:07:53 UTC, user1234 wrote:
 [...]
Hi, Now there is no duplicate , but the sequence is still not correct [...]
Hi, If I execute the script several time's i still get the duplicate entries. From, Vino.B
Aug 26 2017
parent reply Vino.B <vino.bheeman hotmail.com> writes:
On Saturday, 26 August 2017 at 10:53:03 UTC, Vino.B wrote:
 On Saturday, 26 August 2017 at 10:45:13 UTC, Vino.B wrote:
 On Saturday, 26 August 2017 at 10:07:53 UTC, user1234 wrote:
 [...]
Hi, Now there is no duplicate , but the sequence is still not correct [...]
Hi, If I execute the script several time's i still get the duplicate entries. From, Vino.B
Hi, After analyzing a bit further was able to find that the out data before sorting is like below(4 - 2 dimensional array) hence the sorting is not working, so may i know how do i make this as a single 2 dimensional array like below Required: [["C:\\Temp\\TEAM2\\TEAM\\DIR1", "40"], ["C:\\Temp\\TEAM2\\TEAM\\DIR2", "2228"], ["C:\\Temp\\TEAM3\\EXPORT\\dir2", "61"], ["C:\\Temp\\TEAM2\\PROD_TEAM\\dir1", "35772"], ["C:\\Temp\\TEAM2\\BACKUP\\dir1", "35732"], ["C:\\Temp\\TEAM3\\BACKUP\\dir1", "71465"], ["C:\\Temp\\TEAM2\\EXPORT\\dir2", "30"]] Output: [["C:\\Temp\\TEAM2\\TEAM\\DIR1", "40"], ["C:\\Temp\\TEAM2\\TEAM\\DIR2", "2228"], ["C:\\Temp\\TEAM3\\EXPORT\\dir2", "61"]] - 2darray 1 [["C:\\Temp\\TEAM2\\PROD_TEAM\\dir1", "35772"]] - 2darray 2 [["C:\\Temp\\TEAM2\\BACKUP\\dir1", "35732"], ["C:\\Temp\\TEAM3\\BACKUP\\dir1", "71465"]] - 2darray 3 [["C:\\Temp\\TEAM2\\EXPORT\\dir2", "30"]] - 2darray 4 From, Vino.B
Aug 27 2017
parent Vino.B <vino.bheeman hotmail.com> writes:
On Sunday, 27 August 2017 at 11:53:29 UTC, Vino.B wrote:
 On Saturday, 26 August 2017 at 10:53:03 UTC, Vino.B wrote:
 [...]
Hi, After analyzing a bit further was able to find that the out data before sorting is like below(4 - 2 dimensional array) hence the sorting is not working, so may i know how do i make this as a single 2 dimensional array like below [...]
Hi, Thank you very much, was able to resolve the issue. From, Vino.B
Aug 27 2017