Solved

Flex nested ArrayCollections sorting

Posted on 2010-11-25
10
934 Views
Last Modified: 2012-05-10
Hi experts,

I have an ArrayCollection nested in another one. I need to sort the child collection by a field.
I tried the following code. It works fine for the parent collection, but does nothing for the child one.

L0ck3
// The Data 

public class Data
{
  public var value:String;
  public var otherValue:String;
  public var childrenData:ArrayCollection; // An array collection containing Data objects.
} 

// The sorting function 

private function sortData():void
{
  var records:ArrayCollection() = bla bla (init the collection with some Data objects).

  records.sort = new Sort();
  records.sort.fields = [
    new SortField('value', true, false)
  ];

  for each (var data:Data in records){
    sortChildren(data);
  }
  records.refresh();			
}
		
private function sortChildren(data:Data):void
{
  if (data.childrenData != null) {
    var srt:Sort = new Sort();
    srt.fields = [
      new SortField('otherValue', true, false)
    ];
    data.childrenData.sort = srt;
    data.childrenData.refresh();
  }		
}

Open in new window


I can't figure out what I'm doing wrong, so some help would be appreciated.

Thanks in advance,

0
Comment
Question by:L0ck3
10 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 34215875
Can you show us the bla bla code?
>> var records:ArrayCollection() = bla bla (init the collection with some Data objects).
0
 
LVL 2

Author Comment

by:L0ck3
ID: 34215964
In my "real world application", this data comes from a java server via BlazeDS.

The only code for initialization of the collection on Flex side, is the following :

 
records = rpcEvent.result as ArrayCollection;

Open in new window


The class I named "Data" is also a more complex one, but it is not supposed to deal with the problem.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 34216659
My concern is that I see you write this line

>> new SortField('otherValue', true, false)

and I wanted to check if the field 'otherValue' really exist?

In the help file of SortField I read the following:
There are situations in which the collection contains simple items, like String , Date , Boolean , etc.
In this case, sorting should be applied to the simple type directly. When constructing a sort for this situation only a single sort field is required and should not have a name specified.

So - if you are in that case; that your (child)collection only contains Strings - why don't you try:

new SortField(null, true, false)

instead of

>> new SortField('otherValue', true, false)
0
 
LVL 2

Author Comment

by:L0ck3
ID: 34216695
Thanks for answering.

The collection contains other objects of type Data. I want the parent records to be sorted by the "value" fields and the children of each record to be sorted by the "otherValue" field.

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 37

Expert Comment

by:zzynx
ID: 34216736
I see.
Shouldn't the field be specified by a string between double quotes?

new SortField("otherValue", true, false)

instead of

new SortField('otherValue', true, false)
0
 
LVL 2

Author Comment

by:L0ck3
ID: 34217457
I tried, but ir didn't change anything. In AS3 strings can be defined by single or double quotes.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 34217559
>> It works fine for the parent collection, but does nothing for the child one.
How do you check that?
By printing them out? Or by inspecting them in your IDE?

In sortchildren() try printing out

  data.childrenData

to check it contains what you expect (Data objects having the "otherValue" property)
0
 
LVL 1

Assisted Solution

by:Gvoid
Gvoid earned 250 total points
ID: 34241595
The code looks good, so I can understand your frustration.

A couple of things I would try:
1. place a try catch block around the
data.childrenData != null
check and confirm the data is not null

e.g.
private function sortChildren(data:Data):void
{
 try {
  if (data.childrenData != null) {
    var srt:Sort = new Sort();
    srt.fields = [
      new SortField('otherValue', true, false)
    ];
    data.childrenData.sort = srt;
    data.childrenData.refresh();
  } else {
   trace("data.childrenData was found to be null");
  }	
 } catch (e:Error){
  trace(e.message); // or whatever output method your using
  trace("data.childrenData: " + data.childrenData);
 }	
}

Open in new window




2. rework/place the sort function inside the 'Data' class and then call that function from the

for each (var data:Data in records){
    sortChildren(data);
  }

Open in new window


loop instead

so you end up with
for each (var data:Data in records){
    data.sort();
}

Open in new window




0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 34824213
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now