PDA

View Full Version : Sorting a Multidimensional Array by the date(03.11.04)



waffe
March 11th, 2004, 03:13 AM
Hi,

I need a sort function that will read data from an array and sort it by the date - first to last. The date format is (03.11.01) which seems to make it a bit more complex to sort.

Here is my code for loading data into flash via loadVars:



Concert_file = new LoadVars();
Concert_fileURL = "http://www.domain.com/concert_data.txt";
Concert_file.load(Concert_fileURL);
Concert_file.onLoad = function(success) {
if (success) {
ConcertArray = new Array();
for (i=0; i<this.NUMITEMS; i++) {
var cDate = eval("this.cDate"+i);
var cVenue = eval("this.cVenue"+i);
var cCity = eval("this.cCity"+i);
var cWith = eval("this.cWith"+i);
var cInfo = eval("this.cInfo"+i);
arrayMonth = cDate;
arrayMonth = arrayMonth.substr(0, 2);
if (arrayMonth != undefined) {
ConcertArray[i] = [cInfo, cWith, cCity, cVenue, cDate];
}
}
}
};


This is the txt file loadVars loaded:


&cDate7=06.08.04&cVenue7=T Joes&cCity7=Mil, WI&
&cDate8=08.13.04&cVenue8=400bar&cCity8=Min, MN&
&cDate9=07.08.04&cVenue9=T Joes&cCity9=Mil, WI&



I have tried many things and no real results to show. I get lost at the Multidimensional Array part being loaded from loadVars. I see myself parseing the date out of the array and then run the custom sort, but...

Any ideas would be :D

Thanks

waffe
March 11th, 2004, 04:17 PM
Here is some info I got from another flash forum. Still a bit lost!

Firstly the date needs to be treated as a serial date to be compared, to do this Date.UTC() can be used.

Secondly to sort by a number a different sort algorithm needs to be used.

And thirdly the sort needs to work on array items within an array.

Try the code as follows:


function sortDates() {
ConcertArray.sort(byDate); // Uses "byDate" as the algorithm
}

function byDate (a, b) {
return Date.UTC(a[6],a[5],a[4])>Date.UTC(b[6],b[5],b[4]); }


The algorithm takes out the date items of the array a (which is an item in the main array) and compares them with the date items in array b (which is an item in the main array). This is done by comparing the two serial dates.


To sort in the opposite direction change the > to a < in the function above.

The only slight adaptation to you original code is to split the day, month and year into separate items of the array (items 4, 5 and 6).

This is only required to make this code simpler however if it is too tricky to get the date broken down into different variables then a date chopping function can be scripted in actionscript.

------------------------------------------------------------------------------
Can someone please help me put this together.

Thanks

waffe
March 11th, 2004, 09:36 PM
Concert_file = new LoadVars();
Concert_fileURL = "http://www.domain.com/concert_data.txt";
Concert_file.load(Concert_fileURL);
Concert_file.onLoad = function(success) {
if (success) {
ConcertArray = new Array();
for (i=0; i<this.NUMITEMS; i++) {
var cDate = eval("this.cDate"+i);
var cVenue = eval("this.cVenue"+i);
var cCity = eval("this.cCity"+i);
var cWith = eval("this.cWith"+i);
var cInfo = eval("this.cInfo"+i);
var cYear = int(cDate.substr(0, 4));
var cMonth = int(cDate.substr(6, 2));
var cDay = int(cDate.substr(8, 2));

ConcertArray[i] = [cDate, cVenue, cCity, cWith, cInfo, cYear, cMonth, cDay];
}
}
}
function byDate (a, b) {
return Date.UTC(a[5],a[6],a[7])>Date.UTC(b[5],b[6],b[7]);
}
on(press){
ConcertArray.sort(byDate);
}

O YA:p: