Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #19605
| Message-ID | <12786665.ZbpLptdNGN@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-05-03 01:26 +0200 |
| Subject | Re: About keeping my array 1 based after having sorted it |
| Newsgroups | comp.lang.javascript |
| References | <klrbhd$duk$1@dont-email.me> <c0affca3-9adb-40c0-bcdd-3222defbd338@a8g2000yqp.googlegroups.com> |
Scott Sauyet wrote:
> You can probably achieve the same thing with this:
>
> bookingArray.sort(function(a, b) {
> return a.Objectid < b.Objectid ? -1 :
> a.Objectid > b.Objectid ? +1 :
> new Date(a.Fromtime) - new Date(b.Fromtime);
(The “+” in “+1” is a superfluous conversion operation; it is *not* a no-op.
Also, few people know that “-1” actually is a “unary -” conversion operation
on the primitive Number value 1. See ES 5.1, §§ 11.4.6 and 11.4.7.)
Please do not write or suggest such code; it is a maintenance nightmare.
Write such code if you want your colleagues to dislike you for it, put
politely. See also: <http://stackoverflow.com/q/14693135/855543>
The least you should do is using parentheses to group the expressions to
make it easier to read and to debug, for example (preferred by me)
return (a.Objectid < b.Objectid
? -1
: (a.Objectid > b.Objectid
? 1
: new Date(a.Fromtime) - new Date(b.Fromtime)));
or (preferred by others)
return (a.Objectid < b.Objectid)
? -1
: (a.Objectid > b.Objectid)
? 1
: new Date(a.Fromtime) - new Date(b.Fromtime);
As the conversion to Date appears to be superfluous here¹, there is also
var comparator = jsx.array.getComparator(
["Objectid", "Fromtime"], {numeric: true});
bookingArray.sort(comparator);
(see
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=/trunk/array.js>)
The least you should do is cache the comparator.
> });
>
> And if the Objectid properties are numeric, you can get even simpler:
>
>
> bookingArray.sort(function(a, b) {
> return a.Objectid - b.Objectid ||
> new Date(a.Fromtime) - new Date(b.Fromtime);
> });
Again, indicating the operator precedence is a good idea:
bookingArray.sort(function (a, b) {
return (a.Objectid - b.Objectid)
|| (new Date(a.Fromtime) - new Date(b.Fromtime));
});
or (more efficient here)
bookingArray.sort(function (a, b) {
return (a.Objectid - b.Objectid) || (a.Fromtime - b.Fromtime);
});
It should also be noted that using the “||” operator carries with it the
internal conversion of the first operand to Boolean which may be more
expensive than other approaches.
______
¹ However, this gives me the idea that it could be useful that
jsx.array.getComparator() could also be passed a callback or constructor
for each key, or all keys, whose return value would be used as operand
in the comparison. What do you think?
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
About keeping my array 1 based after having sorted it "Tony" <tony.johansson@inport.com> - 2013-05-01 17:18 +0200
Re: About keeping my array 1 based after having sorted it Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-01 08:26 -0700
Re: About keeping my array 1 based after having sorted it "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-05-01 19:43 +0200
Re: About keeping my array 1 based after having sorted it Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-05-01 15:15 -0700
Re: About keeping my array 1 based after having sorted it "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-05-02 09:35 +0200
Re: About keeping my array 1 based after having sorted it Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-02 14:31 +0000
Re: About keeping my array 1 based after having sorted it Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-01 22:12 +0000
Re: About keeping my array 1 based after having sorted it "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-05-02 09:36 +0200
Re: About keeping my array 1 based after having sorted it Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-02 08:52 -0700
Re: About keeping my array 1 based after having sorted it Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-03 01:26 +0200
Re: About keeping my array 1 based after having sorted it Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-03 08:53 -0700
Re: About keeping my array 1 based after having sorted it Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-03 18:39 +0200
Re: About keeping my array 1 based after having sorted it Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-03 11:00 -0700
Sorting arrays of objects (was: About keeping my array 1 based after having sorted it) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-05 01:00 +0200
Re: Sorting arrays of objects (was: About keeping my array 1 based after having sorted it) Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-05 08:53 -0700
Re: About keeping my array 1 based after having sorted it Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-05-04 16:14 +0100
csiph-web