Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Flex nested ArrayCollections sorting

Posted on 2010-11-25
10
Medium Priority
?
950 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
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.

604 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