/    Sign up×
Community /Pin to ProfileBookmark

Need help sorting a date/time array.

I’m needing some help to sort my data array by a timestamp field …..

I have a 2D array “AvatarArr” populated with data from a remote source, but it is basically “Name1, Login Timestamp1, Logout Timestamp1, Location1,Name2, Login Timestamp2…..etc”

I also have the following sort functions defined..

[CODE]function byName (a, b) {return a[0] == b[0] ? 0 : a[0] < b[0] ? -1 : 1}
function byLogin (a, b) {return a[1] == b[1] ? 0 : a[1] < b[1] ? -1 : 1}
function byLogout (a, b) {return a[2] == b[2] ? 0 : a[2] < b[2] ? -1 : 1}
function byLocation (a, b) {return a[3] == b[3] ? 0 : a[3] < b[3] ? -1 : 1}[/CODE]

So when I populate my table with data I can sort the table by any column, for example to sort by name I call

[CODE]AvatarArr.sort(byName);[/CODE]

Now this works EXCEPT my Timestamp data isn’t in a format that makes it easy to sort.

The timestamp comes in as

[QUOTE]

Mon 10/26/2009 at 2:05 am

[/QUOTE]

for example.

Is there a trick or some way of changing the function definition of byLogin to handle sorting a timestamp like this correctly (by date/time) rather than what currently happens which is alphabetically?

Thanks
Richard

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@mrhooOct 26.2009 — if you remove the 'at', you can get an integer timestamp from the string with Date.parse(string)-

[CODE]
return Date.parse(a[1].replace('at',''))-Date.parse(b[1].replace('at',''))[/CODE]

[B]// test[/B]
[CODE]
var A= [
['jane ','Mon 10/19/2009 at 2:06 am'],
['sam ','Tue 10/27/2009 at 2:05 am'],
['bob ','Mon 10/26/2009 at 2:05 am'],
['ann ','Mon 10/19/2009 at 2:05 am']
];

A.sort(function(a, b){
return Date.parse(a[1].replace('at ',''))- Date.parse(b[1].replace('at ',' '))
});[/CODE]


A.join('n')

returned value:

ann ,Mon 10/19/2009 at 2:05 am

jane ,Mon 10/19/2009 at 2:06 am

bob ,Mon 10/26/2009 at 2:05 am

sam ,Tue 10/27/2009 at 2:05 am
Copy linkTweet thisAlerts:
@rpalmer68authorOct 26.2009 — Thanks for the quick reply mrhoo,

This seems to be working as you suggest, the only issue I have now is that if somebody IS currently online the Logout timestamp is just "1" instrad of a valid timestamp.

This mucks up the sorting, what I need to do is have all the "1"'s displayed first if sorted by Logout(those currently oline displayed first) ... maybe if they were "0" instead of "1", would the Date.parse() then work and generate a valid date that would be sorted?

Cheers

Richard
Copy linkTweet thisAlerts:
@mrhooOct 26.2009 — I'd look at the input and handle 1's differently.

Date.parse('1') returns NaN,

//
[CODE]var A= [
['jane ','Mon 10/19/2009 at 2:06 am'],
['sam ','Tue 10/27/2009 at 2:05 am'],
['jim ','1'],
['bob ','Mon 10/26/2009 at 2:05 am'],
['ann ','Mon 10/19/2009 at 2:05 am'],
['stan','1']
];
A.sort(function(a, b){
a= Date.parse(a[1].replace('at ',''));
b= Date.parse(b[1].replace('at ',''));
if(isNaN(a) || isNaN(b)){
return (isNaN(a))? -1: 1;
}
return a-b;
});

alert(A.join('n'))
[/CODE]

//[B] returned value:[/B]

jim ,1

stan,1

ann ,Mon 10/19/2009 at 2:05 am

jane ,Mon 10/19/2009 at 2:06 am

bob ,Mon 10/26/2009 at 2:05 am

sam ,Tue 10/27/2009 at 2:05 am
Copy linkTweet thisAlerts:
@rpalmer68authorOct 26.2009 — Excellent, thanks for that mrhoo.


I have check boxes for what I want to sort by, so I can in fact sort by Name AND Logout.

So this means I could have both of these run.

[CODE]
if (form1.byname.checked) AvatarArr.sort(byName);
if (form1.bylogout.checked) AvatarArr.sort(byLogout);[/CODE]


So this would sort the array by name, and then sort it by the Logout timestamp.

Your suggested code is working if I sort by Name and Logout, but now when I get the table populated I have the users with logout="1" at the top YAY!, BUT they are in reversed order (Z - A) rather than what I need which is A-Z.


So for anybody else reading this and trying to do somethig similar... I changed [CODE]return (isNaN(a))? -1: 1;[/CODE] to [CODE]return isNaN(a)&& isNaN(b)? 0 :(isNaN(a))? -1 : 1;[/CODE] making the full function..[CODE]function byLogout(a, b){
a= Date.parse(a[2].replace('at ',''));
b= Date.parse(b[2].replace('at ',''));
if(isNaN(a) || isNaN(b)){
return isNaN(a)&& isNaN(b)? 0 :(isNaN(a))? -1 : 1;
}
return a - b ;
};[/CODE]




Cheers

Richard
Copy linkTweet thisAlerts:
@rpalmer68authorOct 31.2009 — hmm, the above solution has been working perfectly for the last few days, but today I have a problem.

I have logout dates of "Sat 10/31/2009 at 12:36 am" in amongst my logged in users.

I can only assume date.parse is returning NaN for this date for some reason... could this be possible?

Richard
Copy linkTweet thisAlerts:
@mrhooOct 31.2009 — You are removing the 'at 's, including the 'at ' in 'Sat '-

replace[B] ' at' [/B]instead, to only remove [B]at[/B] if preceded by a space.
Copy linkTweet thisAlerts:
@rpalmer68authorOct 31.2009 — Duh! Of course... I knew it was going to be something simple!

Thanks a lot mrhoo.

Richard
×

Success!

Help @rpalmer68 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...