Second arguments for Array.prototype.sort: map function

Peter Michaux petermichaux at gmail.com
Thu Feb 23 19:08:11 PST 2012


I think I misunderstood your intent. Is what you want equivalent to
the following?

arr.map(function(el){return {a:el,b:f(el)};}).sort(function(x, y)
{return x.b > y.b;}).map(function(el){return el.a;});

Peter

On Thu, Feb 23, 2012 at 6:12 PM, Peter Michaux <petermichaux at gmail.com> wrote:
> On Mon, Feb 20, 2012 at 11:52 AM, Xavier MONTILLET
> <xavierm02.net at gmail.com> wrote:
>
>> what I would like is to be able to do this:
>>
>> var sortedArray = array.sort( compare, f );
>
> I think the above is not very readable.
>
> The following is more readable and shows the order in which the more
> modular parts are being applied.
>
> var sortedArray = array.map(f).sort(compare);
>
> Peter


More information about the es-discuss mailing list