473,809 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 AlphaNumericall y first using

this first:

list_b_array = cf_array.sort(f unction(a,b){re turn
compareAlpha(a[0],b[0]);});

then this:

list_array = list_b_array.so rt(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 1806
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 AlphaNumericall y 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*********@fo o.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.demo n.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.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.supernew s.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 AlphaNumericall y first using

this first:

list_b_array = cf_array.sort(f unction(a,b){re turn
compareAlpha (a[0],b[0]);});

then this:

list_array = list_b_array.so rt(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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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 AlphaNumericall y first using

this first:

list_b_array = cf_array.sort(f unction(a,b){re turn
compareAlpha(a[0],b[0]);});

then this:

list_array = list_b_array.so rt(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(compareDat e))

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*****@agrico reunited.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4311
by: John Taylor | last post by:
I have a ListCtrl with 5 columns. The first 4 columns are either strings or integers, but the last column is a string in the format of MM-DD-YYYY. I searched google and also read over the ColumnSorterMixin code & examples, but still can not figure out how to to write a user-defined sort routine just for the last column in order to sort that column by date. I still want the sorting of the first four columns to work as expected. How is...
10
3300
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: http://www.ipwebdesign.net/kaelisSpace/useful_tableSort.html This works great except it doesn't sort my UK formatted dates properly, and I end up with something like this: Birth Date (dd/mm/yyyy)
22
4180
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b) { return -1; } else
0
851
by: Teis Draiby | last post by:
I have a PropertyGrid in C# that shows the properties of a managed C++ class instance. The properties in the list are not sorted properly in the order I typed them, as I want. "PropertySort" is set to "NoSort" (or "Categorized") but the properties are neither sorted alphabetically or in the order they are defined in C++. Additionaly the grey "Get-only" properties are all in the top of the list, but I also want these to mix with the...
3
1495
by: ScottBH | last post by:
Discovered a fun one that I thought I should share, and hopefully catch Microsoft's attention to update some documentation. I'm a bit new to VB.NET, and was struggling with getting a combo box to work. I was attempting to take advantage of the cool way to load a combo box via a datasource, rather than the old VB6 way of adding selections. First I tried an array of objects. I had some issues with that that I suspect had a lot to do with...
5
5163
by: DW | last post by:
I have a query in Access 2003 that has the following criteria SELECT tblOrder.SessionDate, tblMenus.Item_Name, tblOrder.Type, Format$(tblorder!SessionDate,"Short Time") AS SessionTime, Sum(tblMenus.Item_Quantity) AS MenuCount INTO tblSessionQuery FROM tblOrder, tblMenus WHERE (((tblOrder.Order_ID)=.) AND ((Format$(!,"mm/dd/yyyy")) Between Format$(!!,"mm/dd/yyyy") And Format$(!!,"mm/dd/yyyy")))
4
1702
by: jkuruvil | last post by:
I need to print a report of a bunch of records that falls between certain dates. Some records have dates and some don't. I did a UNION query to look for everything that falls between these two dates and everything that have NULL in date fields. Records get sorted in an ascending order. So, now I do see all the records, but I like to display records without dates in the end with another title. How could I do this.
3
3901
Ericks
by: Ericks | last post by:
I have several subforms (datasheet mode), each with a date. The default dates are that of the system but they can be altered at will by the user. I want these dates to remain sorted descending, that is, the most recent date first and later dates in the subsequent rows. Although I entered Subform. DESC in the subforms Design View "Order by" it keeps mixing up every time I open the main form. I guess the subform sorts by real input date and not by...
10
1467
tuxalot
by: tuxalot | last post by:
I have a table (TblDateHistory) to record lost days due to injuries: EmployeeID StartDate ReturnDate RestrictedOrLostDays TotalDays 1 ... ... Lost Time Days 36 1 ... ... Restricted / Transfer Days 50 2 ... ... Lost Time Days 10 3 ... ... Restricted / Transfer Days 8 1 ...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.