Sorting array or AssocArrays by value.

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sun May 4 07:18:57 PDT 2008


Me Here wrote:
> Problem: That works fine if the values in the origianl array are unique.
> But in this case thay are not. This is frequency information. 
> The index to the array is numeric value of words in a file. 
> The values are counts of the occurance of that value in the file,

Try putting them in an array of structs with overloaded opCmp, and then 
sorting that. For example:
-----
module test;

import tango.io.Stdout;
import tango.text.convert.Format;

struct Pair {
     int a, b;

     int opCmp(Pair* other) {
         if (a != other.a)
             return typeid(typeof(a)).compare(&a, &other.a);
         return typeid(typeof(b)).compare(&b, &other.b);
     }

     char[] toString() {
         return Format("{}:{}", a, b);
     }
}

void main() {
     int[int] aa = [43: 2, 42: 12, 10: 3, 100: 1000, 101: 200, 7: 3];
     Stdout(aa).newline;

     auto arr = new Pair[aa.length];
     size_t i = 0;
     foreach (key, val ; aa)
         arr[i++] = Pair(val, key);

     arr.sort;

     Stdout(arr).newline;
}
-----
output:
=====
{ 100=>1000, 101=>200, 7=>3, 10=>3, 42=>12, 43=>2 }
[ 2:43, 3:7, 3:10, 12:42, 200:101, 1000:100 ]
=====

Notes:
* Tango is only used for formatting and could easily be replaced by 
appropriate Phobos routines.
* I used typeid().compare because simple subtraction might overflow for 
integer data >= int.sizeof.
* You may want to use an unsigned type for the frequency. Just change 
the types of the AA and the Pair member data. Optionally, template Pair 
on the types if you need to do this for multiple data types.



More information about the Digitalmars-d mailing list