Sorting array of string arrays by an element in the string array

neal via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 21 18:58:18 PDT 2014


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.


More information about the Digitalmars-d-learn mailing list