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?
 
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

0
 
dgofmanCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.