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


Groups > comp.lang.javascript > #18785

Re: sort array

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!takemy.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2369961.HIm05VUU83@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Organization PointedEars Software (PES)
Date Sat, 09 Mar 2013 13:01:09 +0100
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: sort array
Newsgroups comp.lang.javascript
References <kh2lrk$ena$1@dont-email.me> <kh3ngq$jn4$1@speranza.aioe.org> <7839584.47tHrR2Uyp@PointedEars.de> <khehj7$2hm$1@speranza.aioe.org>
MIME-Version 1.0
Lines 101
NNTP-Posting-Date 09 Mar 2013 13:01:09 CET
NNTP-Posting-Host 93a8bb25.newsspool2.arcor-online.net
X-Trace DXC=7c9Oim6k4;FA<A_d>@P]\DA9EHlD;3YcB4Fo<]lROoRA8kF<OcfhCOK4LhWYJTITMLDZm8W4\YJNLb@mF9jNikdEIQbgJMhLo0Gkfk^Tlm34@F
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:18785

Show key headers only | View raw


Joao Rodrigues (J.R.) wrote:

> On 05/03/2013 13:51, Thomas 'PointedEars' Lahn wrote:
>> Or you could make Date objects out of the string values (see
>> previous discussions) and sort those.  For example, if you can be
>> sure that the implementation is one of ECMAScript Edition 5 or 5.1,
>> you could replace “/” with “-” and “ ” with “T” and pass that to the
>> Date constructor, because the result is the simplified ISO 8601 time
>> format supported by those implementations (ES 5.1, §15.9.1.5), and
>> Date values of the same time zone sort easily because converting them
>> to Number yields their time value (see also those discussions).
>>
> 
> Theoretical. Remember the code will run in the client-side, while the
> page could be accessed by users from different time zones, or by users
> that have their computers' clocks set to different time zones (which is
> common in maritime companies). Red herring and Argumentum verbosium.

No, *your* argument is the fallacy.  Nobody said where the string values 
come from; they may very well be coming from the server side.  The point is 
that the string values are in a specific format, so you can use that fact to 
your advantage.  Either transform it to a format supported by the Date 
constructor (if you can guarantee that for the target environments), so that 
you can sort time values of Date instances, …

>>> However, you'd be better off testing for the expected ETA string
>>> format passed as the arguments to the sortETA(), using regular
>>> expressions: test whether you have the expected number of digits,
>>> spaces etc. for the date and time parts of the string)
>>
>> Or you could get just the minimum clue and learn that this particular
>> date format (with leading zeroes and fixed length) can be sorted as a
>> string, i.e. *without* such an awfully *inefficient* user-defined
>> comparator:
>>
>> var arr = ['2013/03/06 10:23:01', '2013/03/05 23:59:01', '2013/03/06
>> 00:00:59', '2013/03/05 22:15:00']; arr.sort();

… or sort the strings verbatim if the parts are in positional order, as 
here.
 
> This is known as /defensive programming/.

No, your approach is known as bloat programming.  One simply does not use a 
user-defined comparator when the built-in one suffices.  In this case, all 
values are *strings* (consisting of Unicode characters within the BMP), and 
the array is not sparse, so the built-in comparator suffices.

> As to the "awfully *inefficient* user-defined comparator", there is no
> evidence that different ECMAScript implementations would sort an array
> consisting of strings more efficiently than the sortETA() function.

It suffices to know that for a user-defined comparator you have to create 
another Function instance, and with *your* approach you have to create even 
more objects in the comparator, to know that this approach must be less 
memory-efficient than using the built-in comparator.

It suffices to know that a user-defined function must be called, and that 
*your* comparator does a lot more than “===”, “<”, and “>” comparisons, to 
know that it must be less runtime-efficient than the built-in algorithm.

Therefore, that approach is less efficient than the one that uses the built-
in algorithm.  On top of that, it is less efficient *needlessly* *here*, 
because it provides no additional value *here*.

> Hasty generalization.

Once again you have no clue what you are talking about.  Outside of wannabe 
corners complexity is well enough understood to make the assessment that I 
made about your approach.
 
> See
> <https://developer.mozilla.org/en-
US/docs/JavaScript/Reference/Global_Objects/Array/sort>

Yes, please read that before you post again.  Please read other reference 
material, like the ECMAScript Language Specification, 5.1 Edition, section 
15.4.4.11, too.

>> That said, due to your careless reading your answer does not address
>> the question at all; which is basically how to sort using two sort
>> keys (which should rather be a topic for “Programming 101”).
> 
> You needn't be so unpleasant.

I am merely stating the facts here.  If you do not want to read that you are 
wrong, do not be wrong.

> And until here, you have missed the point altogether.

No, *you* have completely missed the point of the OP which was _not_ how to 
sort date values (in whatever form), but how to use *two* keys for sorting 
an Array.  And you have missed the fact that their dates are in a specific 
format, so in your mistargeted answer you also failed to take advantage of 
that.

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

sort array "Tony" <tony.johansson@inport.com> - 2013-03-04 18:33 +0100
  Re: sort array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-04 19:19 +0100
  Re: sort array "J.R." <groups_jr-1@yahoo.com.br> - 2013-03-05 00:07 -0300
    Re: sort array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-05 17:51 +0100
      Re: sort array "Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br> - 2013-03-09 02:33 -0300
        Re: sort array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-09 13:01 +0100
  Re: sort array Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-05 21:10 +0000

csiph-web