Connecting Tech Pros Worldwide Forums | Help | Site Map

dates not sorted properly

Michael Hill
Guest
 
Posts: n/a
#1: Jul 23 '05
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;
}



Michael Daly
Guest
 
Posts: n/a
#2: Jul 23 '05

re: dates not sorted properly


On 5-May-2004, "Michael Hill" <hillmw@charter.net> wrote:
[color=blue]
> var cf_array = [ "","11/10/2003","11/28/2004","" ];[/color]

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
mscir
Guest
 
Posts: n/a
#3: Jul 23 '05

re: dates not sorted properly


Michael Hill wrote:[color=blue]
> 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[/color]
<snip>

You might give this a try:

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

Dr John Stockton
Guest
 
Posts: n/a
#4: Jul 23 '05

re: dates not sorted properly


JRS: In article <BqKdnVwf_qFYNQTdRVn-ig@magma.ca>, seen in
news:comp.lang.javascript, Michael Daly <michaelDaly@foo.bar> posted at
Thu, 6 May 2004 03:00:00 :[color=blue]
>On 5-May-2004, "Michael Hill" <hillmw@charter.net> wrote:
>[color=green]
>> var cf_array = [ "","11/10/2003","11/28/2004","" ];[/color]
>
>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.[/color]


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.
Dr John Stockton
Guest
 
Posts: n/a
#5: Jul 23 '05

re: dates not sorted properly


JRS: In article <109j8mp7iovuu9c@corp.supernews.com>, seen in
news:comp.lang.javascript, Michael Hill <hillmw@charter.net> posted at
Wed, 5 May 2004 21:28:51 :
[color=blue]
>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.[/color]

ISTM likely that proper test would show that the above is not reliable;
but ICBW.

[color=blue]
> Anyone see anything wrong
>with the compareDate() function?[/color]

It need only contain

return new Date(a) - new Date(b) // or vice versa.
[color=blue]
>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","" ];[/color]

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.
Michael Hill
Guest
 
Posts: n/a
#6: Jul 23 '05

re: dates not sorted properly


>[color=blue]
> You might give this a try:
>
> http://javascript.internet.com/forms/date-sorter.html[/color]

Messier than what I already have.
Grant Wagner
Guest
 
Posts: n/a
#7: Jul 23 '05

re: dates not sorted properly


Michael Hill wrote:
[color=blue]
> 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;
> }[/color]

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


Closed Thread


Similar JavaScript / Ajax / DHTML bytes