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
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;
				}
			}

Open in new window

milesperhour1086Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evcrCommented:
not my code but works for me on a datagrid date column
maybe you can use it.


/a custom sort function for dates
//n.b. this sort only works with British date format
//to change to a different format change the regular expression to check for the format you want
//and change the order of the slices
private function sortDate(row1:Object, row2:Object):Number {       
	 var result:Number;
	 var row1Date:String;
	 var row2Date:String;
	 
	 //get the column to sort from the index of the last clicked column
	 var field:String = dgAuctions.columns[clickedColumn].dataField.toString();
	 
	 //define a regular expression that checks for dates in DD/MM/YYYY format
	 var pattern:RegExp = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/; 
	 
	 /*
	 to sort dates all that is needed is to convert the date to a number
	 that is YYYYMMDD.  The date must be in the format DD/MM/YYYY, e.g. 20/02/2006.
	 There is no need to do a specific number sort as all the converted dates will have
	 the same number of digits, i.e. 8
	 */ 
	 
	 //if the data is not null
	 if(row1[field] != null)
	  //if the data passes the regular expression test then create
	  //the date as YYYYMMDD
	  if(pattern.test(row1[field]))
	   row1Date = row1[field].slice(6) + row1[field].slice(3,5) + row1[field].slice(0,2);
	  else
	   //data is not a valid date so default it to 0
	   row1Date = "0";
	 else
	  //data is empty so default it to 0
	  row1Date = "0";
	 
	 //if the data is not null
	 if(row2[field] != null)
	  //if the data passes the regular expression test then create
	  //the date as YYYYMMDD
	  if(pattern.test(row2[field]))
	  row2Date = row2[field].slice(6) + row2[field].slice(3,5) + row2[field].slice(0,2);
	  else
	  //data is not a valid date so default it to 0
	  row2Date = "0";
	 else
	  //data is empty so default it to 0
	  row2Date = "0";
	
	 /*
	 result = 0 means both dates are the same
	 result = 1 means date 1 is greater than date 2
	 result = -1 means date 2 is greater than date 1
	 */
	 if (row1Date == row2Date)
	  result = 0;
	 else if (row1Date < row2Date)
	  result = -1;
	 else
	  result = 1;
	 
	 return result;
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dgofmanCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Flex

From novice to tech pro — start learning today.