When you apply a sort to a collection we use the collection to find selectedItems, hoping that is faster than scanning the collection ourselves. In your case you do not have unique values in the sort column so selectedItem finds an arbitrary match and things go downhill from there. That's probably worth filing a bug for.
I was able to get your example to work by using selectedIndex like this: private function cc() : void{ array = new ArrayCollection([ {"count" : 1207662300, "wp" : "A"}, {"count" : 1207662300, "wp" : "B"}, {"count" : 1207662300, "wp" : "C"}, {"count" : 1207662300, "wp" : "D"}, {"count" : 1207662300, "wp" : "E"} ]); var sorting : Sort = new Sort(); var sortByCount : SortField = new SortField("count",false,true,true); sorting.fields = [sortByCount]; this.array.sort = sorting; this.array.refresh(); dg.selectedIndex = 3; } ________________________________ From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of shafram Sent: Thursday, May 08, 2008 11:35 AM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: Wierd behaviour with selectedItem on Datagrid Thanks for the workarounds but the problem still exists in Flex 2 even after setting the selectedItem using the selectedIndex. This is also a bug in Flex 3 but at least there seems a workaround. --- In flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com> , "Santiago Gonzales" <[EMAIL PROTECTED]> wrote: > > I'm also not sure why this happens, but if you change the code in > function cc of dg.selectedItem = array[3] to dg.selectedIndex = 3 it > works. > > --- In flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com> , "jmfillman" <jmfillman@> wrote: > > > > Not sure if this is a bug, kind of looks like it, but you can prevent > > this by doing this: > > > > <mx:DataGrid id="dg" dataProvider="{array}" > > itemClick="dg.selectedItem = array[dg.selectedIndex]; ck(event)"> > > > > > > --- In flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com> , "shafram" <shafram@> wrote: > > > > > > Hello I am encountering a strange problem when I try to select an > > item > > > after an initial sort order has been provided. > > > > > > In this example, I have 5 items with all the same count. My initial > > > sorting is on the count field. > > > Once the initial sort has been set, I programmatically select the > > 4th > > > item in my array which is Letter "D". If I then try to click on > > Letter > > > "A" which is right above "D", The alert box will display "D" > > not "A" in > > > Flex 3. > > > > > > In flex 2 the event.target.selectedItem is null and an exception is > > > thrown. > > > > > > Here is the source code. Am I doing something wrong or is this a > > bug in > > > flex? > > > > > > <?xml version="1.0" encoding="utf-8"?> > > > <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml <http://www.adobe.com/2006/mxml> " > > > layout="absolute" creationComplete="cc()"> > > > > > > > > > <mx:Script> > > > <![CDATA[ > > > import mx.controls.Alert; > > > import mx.events.ListEvent; > > > import mx.collections.SortField; > > > import mx.collections.Sort; > > > import mx.collections.ArrayCollection; > > > > > > [Bindable] > > > private var array : ArrayCollection ; > > > > > > > > > private function cc() : void{ > > > > > > array = new ArrayCollection([ > > > {"count" : 1207662300, "wp" : "A"}, > > > {"count" : 1207662300, "wp" : "B"}, > > > {"count" : 1207662300, "wp" : "C"}, > > > {"count" : 1207662300, "wp" : "D"}, > > > {"count" : 1207662300, "wp" : "E"} > > > > > > ]); > > > > > > var sorting : Sort = new Sort(); > > > var sortByCount : SortField = new > > > SortField("count",false,true,true); > > > sorting.fields = [sortByCount]; > > > this.array.sort = sorting; > > > this.array.refresh(); > > > > > > dg.selectedItem = array[3]; > > > > > > > > > } > > > > > > private function ck(event : ListEvent) : void { > > > > > > Alert.show(event.target.selectedItem.wp); > > > > > > } > > > > > > ]]> > > > </mx:Script> > > > > > > > > > <mx:DataGrid id="dg" dataProvider="{array}" itemClick="ck > > (event)"> > > > <mx:columns> > > > <mx:DataGridColumn headerText="Count" > > dataField="count"/> > > > <mx:DataGridColumn headerText="Letter" dataField="wp"/> > > > </mx:columns> > > > </mx:DataGrid> > > > > > > > > > </mx:Application> > > > > > >