?
Solved

Sort Flex 3 ArrayCollection by datetime value

Posted on 2008-11-10
3
Medium Priority
?
1,086 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:milesperhour1086
2 Comments
 
LVL 7

Accepted Solution

by:
evcr earned 500 total points
ID: 22929709
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
 
LVL 29

Expert Comment

by:dgofman
ID: 37478741
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question