www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Sorting array of string arrays by an element in the string array

reply "neal" <nfriedma msudenver.edu> writes:
Just curious if this is possible. I have some data on different 
countries that i have stored in a multidimensional array called 
data[][]. What I want to do is sort data[][] by population which 
happens to be stored in data[i][4] where i is the index to 
iterate from country to country.

I would like something like this:

sort!("a>b")(data[][4]);

Anybody have any suggestions?
Oct 21 2014
next sibling parent reply "Joel" <joelcnz gmail.com> writes:
On Wednesday, 22 October 2014 at 00:32:56 UTC, neal wrote:
 Just curious if this is possible. I have some data on different 
 countries that i have stored in a multidimensional array called 
 data[][]. What I want to do is sort data[][] by population 
 which happens to be stored in data[i][4] where i is the index 
 to iterate from country to country.

 I would like something like this:

 sort!("a>b")(data[][4]);

 Anybody have any suggestions?
More like this: sort!(a.country > b.country)(data[][4]);
Oct 21 2014
parent "Joel" <joelcnz gmail.com> writes:
On Wednesday, 22 October 2014 at 00:36:22 UTC, Joel wrote:
 On Wednesday, 22 October 2014 at 00:32:56 UTC, neal wrote:
 Just curious if this is possible. I have some data on 
 different countries that i have stored in a multidimensional 
 array called data[][]. What I want to do is sort data[][] by 
 population which happens to be stored in data[i][4] where i is 
 the index to iterate from country to country.

 I would like something like this:

 sort!("a>b")(data[][4]);

 Anybody have any suggestions?
More like this: sort!(a.country > b.country)(data[][4]);
Oops, forgot the quotes: sort!("a.country > b.country")(data[][4]);
Oct 21 2014
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
neal:

 Anybody have any suggestions?
Something like this, perhaps? data.sort!q{ a[4] > b[4] }; Bye, bearophile
Oct 21 2014
parent reply "neal" <nfriedma msudenver.edu> writes:
On Wednesday, 22 October 2014 at 01:02:17 UTC, bearophile wrote:
 neal:

 Anybody have any suggestions?
Something like this, perhaps? data.sort!q{ a[4] > b[4] }; Bye, bearophile
Hmmm.. Im getting some interesting results here. So when i put all of the populations in an area and used this code: sort!("a>b")(population); writeln("Top 5 population in 1993: "); for(int i = 0; i < 5;i++) writeln(population[i]); I get: Top 5 population in 1993: 1189550675 916529257 264493898 191658591 156810428 The problem there is that i lose the country that the population is associated with (the top one being China). When I run your code: data.sort!q{ a[4] > b[4] }; for(int i = 0; i < 5; i++) writeln(data[][]); I get this result for the top 5: ["Portugal", "1593140", "-668536", "1993", "9993683", "8"] ["Anguilla", "114", "-536", "1993", "9865", "10"] ["Malawi", "31745", "-25289", "1993", "9862531", "6"] ["Grenada", "2274", "-4890", "1993", "96908", "2"] ["Burkina Faso", "22613", "-38738", "1993", "9688261", "6"] Notice that the top populations arent correct.
Oct 21 2014
parent reply "neal" <nfriedma msudenver.edu> writes:
On Wednesday, 22 October 2014 at 01:58:19 UTC, neal wrote:
 On Wednesday, 22 October 2014 at 01:02:17 UTC, bearophile wrote:
 neal:

 Anybody have any suggestions?
Something like this, perhaps? data.sort!q{ a[4] > b[4] }; Bye, bearophile
Hmmm.. Im getting some interesting results here. So when i put all of the populations in an area and used this code: sort!("a>b")(population); writeln("Top 5 population in 1993: "); for(int i = 0; i < 5;i++) writeln(population[i]); I get: Top 5 population in 1993: 1189550675 916529257 264493898 191658591 156810428 The problem there is that i lose the country that the population is associated with (the top one being China). When I run your code: data.sort!q{ a[4] > b[4] }; for(int i = 0; i < 5; i++) writeln(data[][]); I get this result for the top 5: ["Portugal", "1593140", "-668536", "1993", "9993683", "8"] ["Anguilla", "114", "-536", "1993", "9865", "10"] ["Malawi", "31745", "-25289", "1993", "9862531", "6"] ["Grenada", "2274", "-4890", "1993", "96908", "2"] ["Burkina Faso", "22613", "-38738", "1993", "9688261", "6"] Notice that the top populations arent correct.
Wow sorry I just realized my mistake. the array is an array of strings! data.sort!q{ to!int(a[4]) > to!int(b[4]) }; This code fixes my problem! Thanks for the quick responses guys. you rock!
Oct 21 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
neal:

 data.sort!q{ to!int(a[4]) > to!int(b[4]) };

 This code fixes my problem! Thanks for the quick responses 
 guys. you rock!
That converts string->int many more than once for each string. So if memory is not a problem, consider using a decorate-sort-undecorate pattern: data.schwartzSort!q{ a[4].to!int }; Bye, bearophile
Oct 22 2014
parent reply "Neal" <Nfriedma msudenver.edu> writes:
On Wednesday, 22 October 2014 at 07:38:01 UTC, bearophile wrote:
 neal:

 data.sort!q{ to!int(a[4]) > to!int(b[4]) };

 This code fixes my problem! Thanks for the quick responses 
 guys. you rock!
That converts string->int many more than once for each string. So if memory is not a problem, consider using a decorate-sort-undecorate pattern: data.schwartzSort!q{ a[4].to!int }; Bye, bearophile
Interesting! Thank you. Memory isn't a problem but I would still like to learn how to write more efficient programs. If i posted my code would you be able to give me some advice?
Oct 22 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Neal:

 Interesting! Thank you. Memory isn't a problem
Unfortunately currently the sort-decorate-undercorate in Phobos is not very fast.
 but I would still
 like to learn how to write more efficient programs. If i posted
 my code would you be able to give me some advice?
There is far more than just me around here. If you post code, someone usually can write come comment. Bye, bearophile
Oct 22 2014