Solved

Flex nested ArrayCollections sorting

Posted on 2010-11-25
10
937 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
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.

911 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

20 Experts available now in Live!

Get 1:1 Help Now