On 01/04/12 18:49, Peter van der Zee wrote:
No idea whether this has been discussed before, but I find myself
continuously doing this when sorting arrays with objects:
arr.sort(function(a,b){ if (a.prop< b.prop) return -1; if (a.prop>
b.prop) return 1; return 0; });
Couldn't we add an optional string argument to Array#sort that does
this for us? If supplied do the above, otherwise behave as usual. If
there's already a first arg planned for .sort (I haven't kept up),
make it the second arg.
arr.sort('prop');
I believe this would be better left for a higher-order helper function,
given the sort function already receives a predicate:
function compareProperty(name) { return function(a, b){ return a[name]<
b[name]? -1
: a[name] !==
b[name]? 1
: /* otherwise */
0 }}
arr.sort(compareProperty('prop'))
So, this keeps the `sort' function's semantics simple and straight-forward.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss