469,951 Members | 2,487 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

dates not sorted properly

I have this large array with dates in it. There is a function that sorts it
for me. The only problem is that in order for it to get sorted properly I
have to sort it AlphaNumerically first using

this first:

list_b_array = cf_array.sort(function(a,b){return
compareAlpha(a[0],b[0]);});

then this:

list_array = list_b_array.sort(function(a,b){return
compareDate(a[0],b[0]);});

to get the values in list_array properly sorted. Anyone see anything wrong
with the compareDate() function?

The array is much larger. I've just shown how the date is formatted and that
null values exist.

var cf_array = [ "","11/10/2003","11/28/2004","" ];

function compareDate(a,b)
{
var date_a = new Date(a);
var date_b = new Date(b);
if (date_a < date_b) { return -1; }
else
{
if (date_a > date_b) { return 1; }
else
{ return 0; }
}

function compareAlpha(a,b)
{
//compare alpha charadcters
if ( a.toLowerCase() < b.toLowerCase() ) { return -1; }
if ( a.toLowerCase() > b.toLowerCase() ) { return 1; }
return 0;
}
Jul 23 '05 #1
6 1669
On 5-May-2004, "Michael Hill" <hi****@charter.net> wrote:
var cf_array = [ "","11/10/2003","11/28/2004","" ];


Rather than write a mess of code, just change the date format to
yyyymmdd and sort that. Then switch it back to mm/dd/yyyy.
The code will be easier to read.

Mike
Jul 23 '05 #2
Michael Hill wrote:
I have this large array with dates in it. There is a function that sorts it
for me. The only problem is that in order for it to get sorted properly I
have to sort it AlphaNumerically first using

<snip>

You might give this a try:

http://javascript.internet.com/forms/date-sorter.html

Jul 23 '05 #3
JRS: In article <Bq********************@magma.ca>, seen in
news:comp.lang.javascript, Michael Daly <mi*********@foo.bar> posted at
Thu, 6 May 2004 03:00:00 :
On 5-May-2004, "Michael Hill" <hi****@charter.net> wrote:
var cf_array = [ "","11/10/2003","11/28/2004","" ];


Rather than write a mess of code, just change the date format to
yyyymmdd and sort that. Then switch it back to mm/dd/yyyy.
The code will be easier to read.

Your advice is sound in the first and last sentences. Only. Well,
yyyy/mm/dd and yyyy-mm-dd are also good.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #4
JRS: In article <10*************@corp.supernews.com>, seen in
news:comp.lang.javascript, Michael Hill <hi****@charter.net> posted at
Wed, 5 May 2004 21:28:51 :
I have this large array with dates in it. There is a function that sorts it
for me. The only problem is that in order for it to get sorted properly I
have to sort it AlphaNumerically first using

this first:

list_b_array = cf_array.sort(function(a,b){return
compareAlpha(a[0],b[0]);});

then this:

list_array = list_b_array.sort(function(a,b){return
compareDate(a[0],b[0]);});

to get the values in list_array properly sorted.
ISTM likely that proper test would show that the above is not reliable;
but ICBW.

Anyone see anything wrong
with the compareDate() function?
It need only contain

return new Date(a) - new Date(b) // or vice versa.
The array is much larger. I've just shown how the date is formatted and that
null values exist.

var cf_array = [ "","11/10/2003","11/28/2004","" ];


A silly form of date, liable to be misinterpreted. Use one of the
international (and federal) standard forms YYYYMMDD YYYY-MM-DD, or
YYYY/MM/DD. The problems described in your post amply demonstrate the
folly of not using the standard.

Moreover, new Date() is a moderately expensive operation. You do it
twice per comparison, and for an array of N dates to be sorted there
will probably be o(N*ln(N)) (or similar) comparisons. It would be
better to convert the strings to dates, costing o(N), and then sort that
using function Cf(a, b) { return a-b } .

But, by storing the dates as YYYYMMDD YYYY/MM/DD or YYYY-MM-DD strings,
you can use the default sort.
If you had read the newsgroup FAQ, you could have learned the substance
of the above thereby.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5
>
You might give this a try:

http://javascript.internet.com/forms/date-sorter.html


Messier than what I already have.
Jul 23 '05 #6
Michael Hill wrote:
I have this large array with dates in it. There is a function that sorts it
for me. The only problem is that in order for it to get sorted properly I
have to sort it AlphaNumerically first using

this first:

list_b_array = cf_array.sort(function(a,b){return
compareAlpha(a[0],b[0]);});

then this:

list_array = list_b_array.sort(function(a,b){return
compareDate(a[0],b[0]);});

to get the values in list_array properly sorted. Anyone see anything wrong
with the compareDate() function?

The array is much larger. I've just shown how the date is formatted and that
null values exist.

var cf_array = [ "","11/10/2003","11/28/2004","" ];

function compareDate(a,b)
{
var date_a = new Date(a);
var date_b = new Date(b);
if (date_a < date_b) { return -1; }
else
{
if (date_a > date_b) { return 1; }
else
{ return 0; }
}

function compareAlpha(a,b)
{
//compare alpha charadcters
if ( a.toLowerCase() < b.toLowerCase() ) { return -1; }
if ( a.toLowerCase() > b.toLowerCase() ) { return 1; }
return 0;
}


It's not the compareDate function that's at fault. It's the fact that you're
trying to pass the first array element of the dates (a[0] and b[0]) to
compareDate, when in fact you just want to pass the dates themselves.

var cf_array = [ "","11/28/2004","","11/10/2003","","","12/01/2004","" ];

alert(cf_array.sort(compareDate))

function compareDate(a,b) {
var date_a = new Date(a);
var date_b = new Date(b);
if (!isNaN(date_a) && !isNaN(date_b)) {
return (date_a - date_b);
} else if (isNaN(date_a)) {
return -1;
} else if (isNaN(date_b)) {
return 1;
}
}

You need to deal with the isNaN() issues because new Date("") returns NaN. If
neither the first date nor the second date are NaN, then you just return the
difference (this returns a negative number when date_a is greater then date_b,
positive number when date_a is less then date_b and zero when they are the
same). Otherwise if the first date is NaN, sort it to the top, if the second
date is NaN sort it to the bottom.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Colin Steadman | last post: by
22 posts views Thread by mike | last post: by
3 posts views Thread by ScottBH | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.