www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - aa.byKeyValue().sort!"a.key < b.key"

reply "Nicholas Wilson" <iamthewilsonator hotmail.com> writes:
How do I iterate through an AA sorted by key?

I am unable to .dup the aa.byKeyValue().

I have tried both
foreach(e; aa.byKeyValue().sort!"a.key < b.key")
{
     //... use e. key && e.value
}

and

foreach(k,v; aa.byKeyValue().sort!"a.key < b.key")
{

}

i get :
template std.algorithm.sorting.sort cannot deduce function from 
argument types !("a.key < b.key")(Result), candidates are:
std/algorithm/sorting.d(875):        
std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss 
= SwapStrategy.unstable, Range)(Range r) if ((ss == 
SwapStrategy.unstable && (hasSwappableElements!Range || 
hasAssignableElements!Range) || ss != SwapStrategy.unstable && 
hasAssignableElements!Range) && isRandomAccessRange!Range && 
hasSlicing!Range && hasLength!Range)

any clues?
Jun 27 2015
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sat, Jun 27, 2015 at 12:22:06PM +0000, Nicholas Wilson via
Digitalmars-d-learn wrote:
 How do I iterate through an AA sorted by key?
 
 I am unable to .dup the aa.byKeyValue().
Because it is a range, not an array. To turn it into an array, write: aa.byKeyValue().array.sort!"a.key < b.key" T -- Three out of two people have difficulties with fractions. -- Dirk Eddelbuettel
Jun 27 2015
parent "Nicholas Wilson" <iamthewilsonator hotmail.com> writes:
On Saturday, 27 June 2015 at 12:27:59 UTC, H. S. Teoh wrote:
 On Sat, Jun 27, 2015 at 12:22:06PM +0000, Nicholas Wilson via 
 Digitalmars-d-learn wrote:
 How do I iterate through an AA sorted by key?
 
 I am unable to .dup the aa.byKeyValue().
Because it is a range, not an array. To turn it into an array, write: aa.byKeyValue().array.sort!"a.key < b.key" T
Derp. Thankyou!
Jun 27 2015