| Thread overview | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 05, 2014 sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
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.
| ||||
November 05, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 | |||
November 05, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Meta | 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);
| |||
November 05, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Wednesday, 5 November 2014 at 11:18:56 UTC, Marc Schütz wrote:
> 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);
Ok. Great. What do you think about the name sortBy?
| |||
November 05, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Wednesday, 5 November 2014 at 14:07:10 UTC, Nordlöw wrote:
> On Wednesday, 5 November 2014 at 11:18:56 UTC, Marc Schütz wrote:
>> 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);
>
> Ok. Great. What do you think about the name sortBy?
It's intuitive and concise. Plus, Ruby uses `sort` and `sort_by` for the same functionality, exactly in parallel, so it will be familiar to many users.
| |||
November 05, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | Marc Schütz:
> r.sortBy!(a => a.norm);
r.schwartzSort!(a => a.norm);
Bye,
bearophile
| |||
November 05, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wednesday, 5 November 2014 at 14:52:38 UTC, bearophile wrote:
> Marc Schütz:
>
>> r.sortBy!(a => a.norm);
>
> r.schwartzSort!(a => a.norm);
>
> Bye,
> bearophile
Ok, but this doesn't support multi-sorting, right?
| |||
November 05, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw:
> Ok, but this doesn't support multi-sorting, right?
If performance is not a top priority, you can use a tuple:
r.schwartzSort!(x => tuple(x.a, x.b, x.c));
Bye,
bearophile
| |||
November 05, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Wednesday, 5 November 2014 at 14:36:11 UTC, Marc Schütz wrote: > It's intuitive and concise. Plus, Ruby uses `sort` and `sort_by` for the same functionality, exactly in parallel, so it will be familiar to many users. Here's my first working but primitive version. https://github.com/nordlow/justd/blob/master/sort_ex.d#L15 How do I extend it to support - variadic number of extractors - sortBy("x") ? | |||
November 05, 2014 Re: sortOn: sorts range of aggregates by member name(s) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Wednesday, 5 November 2014 at 16:07:40 UTC, Nordlöw wrote:
> On Wednesday, 5 November 2014 at 14:36:11 UTC, Marc Schütz wrote:
>> It's intuitive and concise. Plus, Ruby uses `sort` and `sort_by` for the same functionality, exactly in parallel, so it will be familiar to many users.
>
> Here's my first working but primitive version.
>
> https://github.com/nordlow/justd/blob/master/sort_ex.d#L15
>
> How do I extend it to support
>
> - variadic number of extractors
> - sortBy("x")
My idea was something along these lines (untested):
template extractorFun(alias extractor) {
static if(is(typeof(extractor) : string)) {
auto ref extractorFun(T)(auto ref T a) {
mixin("with(a) { return " ~ extractor ~ "; }");
}
} else {
alias extractorFun = extractor;
}
}
alias fn = extractorFun!extractor;
r.sort!((a, b) => (fn(a) < fn(b)));
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply