Date Comparison in a Bubble Sort
Author 
Message 
Vick #1 / 8

Date Comparison in a Bubble Sort
I'm using a bubble sort on an array of IDs and Dates that are stored as a string. After sorting on the date, I find that when I render the array, most but not all of the dates are in the correct order. My question is.. is there something wrong with using cdate(x) > cdate(y) or datediff to compare dates? I'm told that it's not perfect and I should use another. I can't think of any so does anyone have any ideas? Perhaps it's the sort code that has an error. This is what my bubble sort code looks like: for i = 0 to uBound(arrNA)  1 for j= 0 to i if cdate(arrNA(j,1)) > cdate(arrNA(j+1,1)) then temp0 = arrNA(j+1,0) arrNA(j+1, 0) = arrNA(j,0) arrNA(j, 0) = temp0 temp1 = arrNA(j+1,1) arrNA(j+1, 1) = arrNA(j,1) arrNA(j, 1) = temp1 end if next next Thanks, Vicky

Sun, 20 Jun 2004 23:49:11 GMT 


MVP #2 / 8

Date Comparison in a Bubble Sort
The range of the inner For...Next loop should be: for i = 0 to uBound(arrNA)  1 for j = i + 1 to uBound(arrNA)  Michael Harris Microsoft.MVP.Scripting  Quote:
> I'm using a bubble sort on an array of IDs and Dates that are stored as a > string. > After sorting on the date, I find that when I render the array, most but not > all of > the dates are in the correct order. My question is.. is there something > wrong with > using cdate(x) > cdate(y) or datediff to compare dates? I'm told that it's > not > perfect and I should use another. I can't think of any so does anyone have > any ideas? > Perhaps it's the sort code that has an error. > This is what my bubble sort code looks like: > for i = 0 to uBound(arrNA)  1 > for j= 0 to i > if cdate(arrNA(j,1)) > cdate(arrNA(j+1,1)) then > temp0 = arrNA(j+1,0) > arrNA(j+1, 0) = arrNA(j,0) > arrNA(j, 0) = temp0 > temp1 = arrNA(j+1,1) > arrNA(j+1, 1) = arrNA(j,1) > arrNA(j, 1) = temp1 > end if > next > next > Thanks, > Vicky

Mon, 21 Jun 2004 01:13:15 GMT 


Vick #3 / 8

Date Comparison in a Bubble Sort
Thanks, but that still gave a notsoperfect sort. Actually, using uBound(arrNA) will give me a subscript error as the if statement after that was trying to reference one item higher than the size of the array. I used for i = 0 to uBound(arrNA)  1 for j = i + 1 to uBound(arrNA) 1 and also for i = 0 to uBound(arrNA)  1 for j = i to uBound(arrNA)  1 and they both weren't quite flawless in the sort. Vicky
The range of the inner For...Next loop should be: for i = 0 to uBound(arrNA)  1 for j = i + 1 to uBound(arrNA)  Michael Harris Microsoft.MVP.Scripting 
Quote: > I'm using a bubble sort on an array of IDs and Dates that are stored as a > string. > After sorting on the date, I find that when I render the array, most but not > all of > the dates are in the correct order. My question is.. is there something > wrong with > using cdate(x) > cdate(y) or datediff to compare dates? I'm told that it's > not > perfect and I should use another. I can't think of any so does anyone have > any ideas? > Perhaps it's the sort code that has an error. > This is what my bubble sort code looks like: > for i = 0 to uBound(arrNA)  1 > for j= 0 to i > if cdate(arrNA(j,1)) > cdate(arrNA(j+1,1)) then > temp0 = arrNA(j+1,0) > arrNA(j+1, 0) = arrNA(j,0) > arrNA(j, 0) = temp0 > temp1 = arrNA(j+1,1) > arrNA(j+1, 1) = arrNA(j,1) > arrNA(j, 1) = temp1 > end if > next > next > Thanks, > Vicky

Mon, 21 Jun 2004 02:01:22 GMT 


Kevin Tucke #4 / 8

Date Comparison in a Bubble Sort
You've got your outside loop going the wrong way. You need to have it start from the bottom and move up, because you're "bubbling" your last date down to the bottom, so it's in it's correct place, not the top one. Also note that your outer loop doesn't need to go all the way to 0. for i = (uBound(arrNA)  1) to 1 step 1 for j= 0 to i if cdate(arrNA(j,1)) > cdate(arrNA(j+1,1)) then temp0 = arrNA(j+1,0) arrNA(j+1, 0) = arrNA(j,0) arrNA(j, 0) = temp0 temp1 = arrNA(j+1,1) arrNA(j+1, 1) = arrNA(j,1) arrNA(j, 1) = temp1 end if next next
Quote: > I'm using a bubble sort on an array of IDs and Dates that are stored as a > string. > After sorting on the date, I find that when I render the array, most but not > all of > the dates are in the correct order. My question is.. is there something > wrong with > using cdate(x) > cdate(y) or datediff to compare dates? I'm told that it's > not > perfect and I should use another. I can't think of any so does anyone have > any ideas? > Perhaps it's the sort code that has an error. > This is what my bubble sort code looks like: > for i = 0 to uBound(arrNA)  1 > for j= 0 to i > if cdate(arrNA(j,1)) > cdate(arrNA(j+1,1)) then > temp0 = arrNA(j+1,0) > arrNA(j+1, 0) = arrNA(j,0) > arrNA(j, 0) = temp0 > temp1 = arrNA(j+1,1) > arrNA(j+1, 1) = arrNA(j,1) > arrNA(j, 1) = temp1 > end if > next > next > Thanks, > Vicky

Mon, 21 Jun 2004 02:10:09 GMT 


MVP #5 / 8

Date Comparison in a Bubble Sort
You'll also need to adjust the array indices within the compare/swap logic according. Here's a simple WSH hosted example that puts the array into ascending date order... Dim arrNA(3,1) arrNA(0,0) = "zero" arrNA(0,1) = date() arrNA(1,0) = "one" arrNA(1,1) = date() 1 arrNA(2,0) = "two" arrNA(2,1) = date() 2 arrNA(3,0) = "three" arrNA(3,1) = date() 3 for i = 0 to uBound(arrNA)  1 for j= i + 1 to uBound(arrNA) if cdate(arrNA(i,1)) > cdate(arrNA(j,1)) then temp0 = arrNA(j,0) arrNA(j, 0) = arrNA(i,0) arrNA(i, 0) = temp0 temp1 = arrNA(j,1) arrNA(j, 1) = arrNA(i,1) arrNA(i, 1) = temp1 end if next next for i = 0 to uBound(arrNA) s = s & arrNA(i,1) & vbcrlf next msgbox s  Michael Harris Microsoft.MVP.Scripting  Quote:
> Thanks, but that still gave a notsoperfect sort. > Actually, using uBound(arrNA) will give me a subscript > error as the if statement after that was trying to reference > one item higher than the size of the array. I used > for i = 0 to uBound(arrNA)  1 > for j = i + 1 to uBound(arrNA) 1 > and also > for i = 0 to uBound(arrNA)  1 > for j = i to uBound(arrNA)  1 > and they both weren't quite flawless in the sort. > Vicky
> The range of the inner For...Next loop should be: > for i = 0 to uBound(arrNA)  1 > for j = i + 1 to uBound(arrNA) >  > Michael Harris > Microsoft.MVP.Scripting > 
> > I'm using a bubble sort on an array of IDs and Dates that are stored as a > > string. > > After sorting on the date, I find that when I render the array, most but > not > > all of > > the dates are in the correct order. My question is.. is there something > > wrong with > > using cdate(x) > cdate(y) or datediff to compare dates? I'm told that it's > > not > > perfect and I should use another. I can't think of any so does anyone have > > any ideas? > > Perhaps it's the sort code that has an error. > > This is what my bubble sort code looks like: > > for i = 0 to uBound(arrNA)  1 > > for j= 0 to i > > if cdate(arrNA(j,1)) > cdate(arrNA(j+1,1)) then > > temp0 = arrNA(j+1,0) > > arrNA(j+1, 0) = arrNA(j,0) > > arrNA(j, 0) = temp0 > > temp1 = arrNA(j+1,1) > > arrNA(j+1, 1) = arrNA(j,1) > > arrNA(j, 1) = temp1 > > end if > > next > > next > > Thanks, > > Vicky

Mon, 21 Jun 2004 03:18:40 GMT 


Branimir Petrovi #6 / 8

Date Comparison in a Bubble Sort
...or switch your task to JScript and have Array object do a "quick sort" magic at native (C++) speeds... Branimir

Mon, 21 Jun 2004 04:08:06 GMT 


MVP #7 / 8

Date Comparison in a Bubble Sort
Except that this a 2 dimensional array which JScript does not support...  Michael Harris Microsoft.MVP.Scripting  Quote:
> ...or switch your task to JScript and have Array object do a > "quick sort" magic at native (C++) speeds... > Branimir

Mon, 21 Jun 2004 04:24:40 GMT 


Branimir Petrovi #8 / 8

Date Comparison in a Bubble Sort
Quote:
>Except that this a 2 dimensional array which JScript does not support...
/* Quite true, but with little "helper" functions, this can be arranged too (at price of having to can kiss native speed up gains good bye). Depending on point of view, I'd say this solution is easier on eyes (thanks to number of curly braces and semi colons ;). */ var oToday = new Date(); var oMinus1 = new Date(oToday.valueOf()  1*86400000); var oMinus2 = new Date(oToday.valueOf()  2*86400000); var oMinus3 = new Date(oToday.valueOf()  3*86400000); var oAry = new Array( new Array("C", oMinus2), new Array("C", oMinus3), new Array("Z", oMinus1), new Array("A", oToday), new Array("B", oMinus1), new Array("B", oToday), new Array("E", oMinus1), new Array("G", oMinus2), new Array("C", oToday), new Array("C", oMinus1), new Array("D", oMinus3), new Array("D", oToday), new Array("A", oMinus1) ); //  WScript.Echo(format(oAry.sort(byDate))); // <<< sort by ascending dates WScript.Echo(format(oAry.sort(byID))); // <<< sort by ascending ID's // (without disturbing sort by dates) function byDate(a, b) { if (a[1]==b[1]) return 0; return (a[1]>b[1])? 1: 1; // change to "<" for ascending ordr Quote: }
function byID(a, b) { if ( (a[1]!=b[1])(a[0]==b[0]) ) return 0; return (a[0]>b[0])? 1: 1; // change to "<" for ascending ordr Quote: }
//  function format(oAry) { // "beautifying" results var sMsg = ""; for (var i=0; i<oAry.length; i++) sMsg += oAry[i] + "\n"; return sMsg; Quote: }
// Branimir

Mon, 21 Jun 2004 08:07:01 GMT 


