Solved

Flex nested ArrayCollections sorting

Posted on 2010-11-25
10
940 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

808 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