?
Solved

Flex nested ArrayCollections sorting

Posted on 2010-11-25
10
Medium Priority
?
948 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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 how to record live broadcast.
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.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

770 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