Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19614

Re: About keeping my array 1 based after having sorted it

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <4320221.o7mTL0IePv@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Organization PointedEars Software (PES)
Date Fri, 03 May 2013 18:39:55 +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+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
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> <12786665.ZbpLptdNGN@PointedEars.de> <bd93fc08-185a-4614-91c2-a78ea5b6beaa@n11g2000yqe.googlegroups.com>
MIME-Version 1.0
Lines 140
NNTP-Posting-Date 03 May 2013 18:39:57 CEST
NNTP-Posting-Host 66dae8f2.newsspool3.arcor-online.net
X-Trace DXC=1o8`[E@Z]D@j7E:bke<5HFMcF=Q^Z^V3H4Fo<]lROoRA8kF<OcfhCOKC?h^emQ26NKDZm8W4\YJNLb@mF9jNikdEBoKGPB=12AG9D9C8NaKClI
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:19614

Show key headers only | View raw


Scott Sauyet wrote:

> Thomas 'PointedEars' Lahn wrote:
>> 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.
> 
> ACK.
> 
> But I choose to accept the very minor performance degradation in order
> to keep the code more obvious, stressing that I'm returning a positive
> number in one place and a negative in another.

KISS: Numbers that have no negative sign are positive.  (Yes, I know about 
zero.)

>> 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.)
> 
> What's your evidence for that?

I remember to have observed this misconception several times before.

> I'm surprised to learn that few people know that.  Do they expect it's
> simply part of the number literal instead?

Apparently so.  I would expect many people to be surprised that

  var x = {-1: "foo"};

is syntactically wrong, while

  var x = {1: "foo"};

is correct, as it is somewhat counter-intuitive.  (A number is a number, 
isn't it?)

>> 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>
> 
> While I do usually avoid nested ternary statements, this type of sort
> function, because of their extreme simplicity, are often an exception.

Of course.  The problem is not the Conditional *Operator* (_not_ “ternary 
statements”).

>> 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);
> 
> I find both of those harder to read than my suggestion.  Can you
> suggest any objective criteria that makes those suggestions easier to
> read or debug?

Yes.  As for reading, the parentheses make it clear what are either the 
conditional expressions or the conditions of those expressions.  As for 
debugging, each value is on its own line, therefore this code is easier to 
debug step-by-step with debuggers that debug by line instead of by 
expression.

>> var comparator = jsx.array.getComparator(
>> ["Objectid", "Fromtime"], {numeric: true});
>>
>> bookingArray.sort(comparator);
>>
>> (see
>> 
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=/trunk/...>)
> 
> That looks to be very useful.

Thank you.

>> or (more efficient here)
>>
>> bookingArray.sort(function (a, b) {
>> return (a.Objectid - b.Objectid) || (a.Fromtime - b.Fromtime);
>> });
> 
> See above.  I'm guessing that `Fromtime` is a string.

Previous threads started by the same person on the same problem (BTW a bad 
idea) suggest that it is a Number value.

>> ______
>> ¹  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?
> 
> I'm not sure what you would mean by "or all keys" unless you mean for
> each independently,

Currently you can configure the sort method (numeric or generic), sort order 
(ascending or descending), and strictness (strict or loose equality) of the 
sorting both for each key separately and for all keys specified in the 
factory call.  The same approach would be applied to the 
callback/constructor options.

> but other than that, I can see that this would be a useful option.

ACK

> If the objects you want to sort have a property more complex than a String
> or Number but that still has an intrinsic order, it would be nice to be
> able to include that in your comparator.  But rather than a
> callback/constructor, how about recursively applying another comparator
> for that key?

I do not see how this could work.  The elements of the Array are to be 
sorted by a property of the objects referred by the Array elements; the 
properties of the object's that are referred by the Array's elements are not 
to be modified.

-- 
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


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