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


Groups > comp.lang.javascript > #18781

Re: sort array

From "Joao Rodrigues (J.R.)" <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: sort array
Date 2013-03-09 02:33 -0300
Organization Aioe.org NNTP Server
Message-ID <khehj7$2hm$1@speranza.aioe.org> (permalink)
References <kh2lrk$ena$1@dont-email.me> <kh3ngq$jn4$1@speranza.aioe.org> <7839584.47tHrR2Uyp@PointedEars.de>

Show all headers | View raw


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.

>> 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();
>

This is known as /defensive programming/. 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. Hasty generalization.

See 
<https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort>


> 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. And until here, you have missed the point
altogether.


> The simplest answer to that is, for example (for ascending order in
> both cases):
>
> tempArray.sort(function (a, b) { /* first key */ var ax = a.x; var bx
> = b.x;
>
> if (ax < bx) { return -1; }
>
> if (ax > bx) { return 1; }
>
> /* else ax == bx */
>
> /* second key */ var ay = a.y; var by = b.y;
>
> if (ay < by) { return -1; }
>
> if (ay > by) { return 1; }
>
> return 0; });
>
> In this case it can be slightly optimized to
>
> tempArray.sort(function (a, b) { /* first key */ var ax = a.x; var bx
> = b.x;
>
> /* * assuming ax and bx are Number values or convertible; *
> alternative: * *   var diff = ax - bx; *   if (diff) { return diff;
> } */ if (ax != bx) { return ax - bx; }
>
> /* else ax == bx */
>
> /* second key, non-numeric */ var ay = a.y; var by = b.y;
>
> if (ay < by) { return -1; }
>
> if (ay > by) { return 1; }
>
> return 0; });
>
> Of course, thanks to the bracket property accessor and loops this can
> be generalized, which has been done before.

ACK.

-- 
Joao Rodrigues (J.R.)

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