Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <12786665.ZbpLptdNGN@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Fri, 03 May 2013 01:26:51 +0200 User-Agent: KNode/4.8.5 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. MIME-Version: 1.0 Lines: 83 NNTP-Posting-Date: 03 May 2013 01:26:53 CEST NNTP-Posting-Host: 97180055.newsspool1.arcor-online.net X-Trace: DXC=N81@XO:S0[FAa;:RKVJ>LEic==]BZ:afN4Fo<]lROoRAnkgeX?EC@@@PmZQ?n;CHJEDZm8W4\YJNLb@mF9jNikdEBoKGPB=12AGMLI;EJO37HC X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:19605 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: 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 ) 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.