Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18717
| From | "J.R." <groups_jr-1@yahoo.com.br> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: sort array |
| Date | 2013-03-05 00:07 -0300 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <kh3ngq$jn4$1@speranza.aioe.org> (permalink) |
| References | <kh2lrk$ena$1@dont-email.me> |
On 04/03/2013 14:33, Tony wrote:
> I have an array called tempArray that consist of these fields
> VesselID, ETATime, Gotref, Point
>
> When I want to sort on VesselID I use which works good
> tempArray.sort(function (a, b)
> {
> return a.VesselID - b.VesselID
> })
>
>
> Now I still want to sort on VesselID but within VesselID I also want to
> sort on ETATime
> ETATime which is of format YYYY/MM/DD hh:mm:ss
>
> How can I do that ?
Assuming that the ETATime string will always be in that format
(yyyy/mm/dd hh:mm:ss), then we might use a function like this:
function sortETA(a, b) {
// a and b are strings representing a date-time
// of format yyyy/mm/dd hh:mm:ss
var e1, e2, re = /[/:\s]/g;
e1 = a.replace(re, ''); // converts to yyyymmddhhmmss
e2 = b.replace(re, ''); // idem
if (e1 === e2) { return 0; }
return (e1 < e2) ? -1 : 1;
}
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(sortETA);
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)
--
Joao Rodrigues (J.R.)
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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