Sorting array or AssocArrays by value.
bearophile
bearophileHUGS at lycos.com
Sun May 4 17:25:03 PDT 2008
Me Here:
> Oh. I guess I was confused by the implementation:
> void valGetter() { } /// ditto
> I couldn't for the life of me se how that would work as is, so I assumed it
> must be some kind of virtual functio or place holder.
> Actually, I /still/ don't understand how it can work, but if you say it does I
> will try it.
sortedAA() accepts any callable with key+val arguments, and sorts the keys of the given AA according to the result of that callable, like the key attribute of Python sort()/sorted().
If the callable is the default valGetter, then the result of that callable is meant to be right the value. So it sorts according to the values. That's quite similar to the itemgetter() and similar functions in the operator module of the Python standard library.
If you try the given example, or you look inside the tens of unittests, you can see how and why it works. It's just an empty function, used as flag, to simplify the life of the user.
> What the convention of where to place thrid-party libraries?
> The stem (root, path; package?) of the librabry names 'd.*' seems um.. a little
> generic?
The package is called "d". I know no other package with that name that may collide with it. You can put that directory inside the directory of your modules, libs, etc. Then you have to tell "bud" (or similar tool, there's another more modern one, that I don't use) the path of that directory of your modules.
Then at the top of your code you can use:
import d.func: sortAA;
And bud will find the right module and its dependent modules, and compile them. If you need more compilation speed you can compile the d libs in a library "lib" and maybe create "short" modules, all using the DMD compiler and its tools.
Bye,
bearophile
More information about the Digitalmars-d
mailing list