Sorting array or AssocArrays by value.
Me Here
p9e883002 at sneakemail.com
Sun May 4 07:45:16 PDT 2008
bearophile wrote:
> Me Here:
> > I have (currently) an array of uints. I need to output these sorted, which
> > .sort does admirably,
> > but I need to know what index is associated with each value. How?
>
> My libs are designed to solve such problems too, take a look at sortedAA
> inside the func module: http://www.fantascienza.net/leonardo/so/libs_d.zip
>
> Bye,
> bearophile
sortAA() looks like it would do the trick, but could you expand on the example
a litle for me. I'm sure the sub signature should tell me all I need to know,
but I'm having a problem working out how to call it?
--------
TyKey[] sortedAA(TyKey,TyVal,TyFun)(TyVal[TyKey] aa, TyFun key);
void valGetter();
Return the keys of an AA sorted according to the result of the given
function/delegate 'key' applied on two arguments, the key and value.
'key' can be any callable, or it can be the predefined &valGetter to sort the
AA keys according to the values.
Example:
Sort the keys of an AA according to the values:
int[string] a = ["DD":3, "aa":1, "BB":2];
sortedAA(d, &valGetter) ==> ["aa", "BB", "DD"]
If you use this to sort an AA according to values, using a valGetter function,
you are paying just about 5% speed penality.
Throws ArgumentException if key returns void.
----------------
If I define my hash as
uint freq[ ushort ];
How do I write valGetter() ?
Cheers, b.
--
More information about the Digitalmars-d
mailing list