milesperhour1086
asked on
Sort Flex 3 ArrayCollection by datetime value
How do you sort datetimes in a flex3 data object? I have an ArrayCollection with many arrays that look like this: {time:2008-08-06 01:00:00-07, KwH:30000} and I need to sort the arraycollection on the time field in each array.
Here is all the code that is generating this problem for me
Here is all the code that is generating this problem for me
private function generate_time_series_average():void
{
var all_devices:ArrayCollection = new ArrayCollection();
for each(var ds:Object in data_services)
{
var the_ds:XML = new XML(ds.data_service.lastResult);
for each(var reading:XML in the_ds.children())
{
all_devices.addItem({time:reading.time, KwH:reading.KwH});
}
}
// Sort by datetime then by did
var timeSortField:SortField = new SortField();
timeSortField.name = "time";
timeSortField.compareFunction = date_sort_function;
var all_devices_sort:Sort = new Sort();
all_devices_sort.fields = [timeSortField];
all_devices.sort = all_devices_sort;
all_devices.refresh();
// Loop through sorted data to generate averages..add to time_series_average
var count:int = 0;
var total:int = 0;
for(var i:int = 0; i < all_devices.length; i++)
{
if(count == data_services.length)
count = 0;
else
count = count + 1;
var this_item:Object = all_devices.getItemAt(i);
total = total + parseInt(this_item.KwH);
if(count == data_services.length)
{
var beforeCalc:int = total;
total = (total / count);
Alert.show("Adding this total for " + this_item.time + ": " + total + "\nIt was calculated like this: (" + beforeCalc + "/" + count + ")");
time_series_average.addItem({time:this_item.time, KwH:total});
count = 0;
total = 0;
}
}
}
private function date_sort_function(a:Object, b:Object):int
{
/*
var tempDateA:Date = new Date(Date.parse(this.fix_a_date(a.toString())));
var tempDateB:Date = new Date(Date.parse(this.fix_a_date(b.toString())));
return ObjectUtil.dateCompare(tempDateA, tempDateB);
*/
var d1ms:Number = new Date(a.time).getTime();
var d2ms:Number = new Date(a.time).getTime();
if(d1ms > d2ms)
{
return -1;
}
else if(d1ms < d2ms)
{
return 1;
}
else
{
return 0;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.