On 11/5/14 12:18 PM, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schue...@gmx.net>" wrote:
On Wednesday, 5 November 2014 at 00:34:54 UTC, Meta wrote:
On Wednesday, 5 November 2014 at 00:32:32 UTC, Nordlöw wrote:
Has there been any proposals to add a sort-wrapper, say sortBy,

in cases such as

   struct X { double x, y, z; }
   auto r = new X[3];

used as

   r.sortBy!("x", "y")

sorting r by value of "x" then "y".

If not and anybody is interest I could write one and make PR in
std.algorithm.

I think you're looking for multiSort.

http://dlang.org/phobos/std_algorithm.html#.multiSort

That's not the same, it requires to specify a comparison function.
Nordlöw wants to specify an attribute.

This could also be an arbitrary expression, of course:

     r.sortBy!"x*x + y*y + z*z"

The above could be implemented using `with` and
`std.functional.unaryFun`. Alternatively, a lambda could be used:

     r.sortBy!(a => a.norm);

sortBy!(expr) is equivalent with sort!(expr_a < expr_b) so there's no additional power to it. However, it does make some sorting criteria easier to write. A while ago I took a related diff pretty far but in the end decided to discard it because it didn't add sufficient value.

Andrei

Reply via email to