Jump to page: 1 2 3
Thread overview
sortOn: sorts range of aggregates by member name(s)
Nov 05, 2014
Nordlöw
Nov 05, 2014
Meta
Nov 05, 2014
Marc Schütz
Nov 05, 2014
Nordlöw
Nov 05, 2014
Marc Schütz
Nov 05, 2014
Nordlöw
Nov 05, 2014
Marc Schütz
Nov 05, 2014
Nordlöw
Nov 05, 2014
Marc Schütz
Nov 06, 2014
Nordlöw
Nov 06, 2014
Marc Schütz
Nov 05, 2014
Nordlöw
Nov 06, 2014
Nordlöw
Nov 05, 2014
bearophile
Nov 05, 2014
Nordlöw
Nov 05, 2014
bearophile
Nov 09, 2014
Nordlöw
Nov 09, 2014
bearophile
Nov 10, 2014
bearophile
Nov 10, 2014
Nordlöw
Nov 10, 2014
Marc Schütz
Nov 10, 2014
Marc Schütz
November 05, 2014
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
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
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
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
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
Marc Schütz:

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

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

Bye,
bearophile
November 05, 2014
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
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
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
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)));
« First   ‹ Prev
1 2 3