Solved

AS3 ---  Sorting Array

Posted on 2011-05-12
8
395 Views
Last Modified: 2012-05-11

I am loading an XML file to an Array and then trying to sort it.

Here is one item of many in the XML file:
 
  <main>
<item>
        <string0>fvg</string0>
        <inside>
          <string1>abc</string1>
          <string2>xyz</string2>
        </inside>
      </main>
<item>

Open in new window


I can sort it with no problem on <string0> but I don't know how to sort it on <string1> or <string2>


0
Comment
Question by:Angha110
  • 4
  • 4
8 Comments
 
LVL 29

Expert Comment

by:dgofman
Comment Utility
If you are using Adobe Flex 4 this code should work for you

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" creationComplete="application1_creationCompleteHandler(event)">
      <fx:Script>
            <![CDATA[
                  import mx.collections.Sort;
                  import mx.collections.SortField;
                  import mx.collections.XMLListCollection;
                  import mx.events.FlexEvent;

                  protected function application1_creationCompleteHandler(event:FlexEvent):void
                  {
                        var sort:Sort = new Sort();
                        sort.fields = [new SortField("string", true)];
                        
                        //Sort parents first
                        var item:XMLListCollection = new XMLListCollection(source.item);
                        item.sort = sort;
                        item.refresh();

                        trace("\nSort by parent:\n" + item);
                        
                        //Sort sub notes
                        sort.fields = [new SortField(null, true)];
                        for each (var inside:XML in item.source.inside){
                              var xml:XMLListCollection = new XMLListCollection(new XMLList(inside.children()));
                              xml.sort = sort;
                              xml.refresh();
                              inside.setChildren(xml.copy());
                        }
                        
                        trace("\nSort by parent and children:\n" +item);
                  }

            ]]>
      </fx:Script>
      
      <fx:Declarations>
            <fx:XML id="source">
                  <main>
                        <item>
                              <string>fvg1</string>
                              <inside>
                                    <string>1</string>
                                    <string>3</string>
                                    <string>2</string>
                              </inside>
                        </item>
                        <item>
                              <string>fvg3</string>
                              <inside>
                                    <string>11</string>
                                    <string>33</string>
                                    <string>22</string>
                              </inside>
                        </item>
                        <item>
                              <string>fvg2</string>
                              <inside>
                                    <string>111</string>
                                    <string>333</string>
                                    <string>222</string>
                              </inside>
                        </item>
                  </main>
            </fx:XML>
      </fx:Declarations>
</s:Application>
0
 

Author Comment

by:Angha110
Comment Utility
I am using Flash CS4
0
 
LVL 29

Expert Comment

by:dgofman
Comment Utility
Ok try this

function toArray(list:XMLList):Array{
	var a:Array = [];
	for(var i:uint = 0; i < list.length(); i++)
		a.push(list[i]);
	return a;
};

for each (var item1:XML in source){
	var children1:Array = toArray(item1.children());
	children1.sortOn("string");
	item1.setChildren(new XMLList());
	for each (var c1:XML in children1){
		for each (var item2:XML in c1.inside){
			var children2:Array = toArray(item2.children());
			children2.sort();
			item2.setChildren(new XMLList());
			for each (var c2:XML in children2){
				item2.appendChild(c2);
			}
		}
		item1.appendChild(c1);
	}
}

trace(item1);

Open in new window

0
 

Author Comment

by:Angha110
Comment Utility
Thank you for the code. I am going to try it right now.
Just to be clear, what if we have another nested tag in our XML file. do we need another loop for that.

 
<main>
      <item>
        <string0>fvg</string0>
           <inside>
              <deep>
                <string1>abc</string1>
                 <string2>xyz</string2>
              </deep>
           </inside>
     <item>
 </main>

Open in new window

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

 
LVL 29

Expert Comment

by:dgofman
Comment Utility
For every nested tag you have to add for loop logic code is similar to this

for each (var item3:XML in c2.deep){
                  var children3:Array = toArray(item3.children());
                  children3.sort();
                  item3.setChildren(new XMLList());
                  for each (var c3:XML in children3){
                        item3.appendChild(c3);
                  }
            }
0
 

Author Comment

by:Angha110
Comment Utility
Please see if this is right (having one more nested tag)

function toArray(list:XMLList):Array{
	var a:Array = [];
	for(var i:uint = 0; i < list.length(); i++)
		a.push(list[i]);
	return a;
};

for each (var item1:XML in source){
	var children1:Array = toArray(item1.children());
	children1.sortOn("string");
	item1.setChildren(new XMLList());
	for each (var c1:XML in children1){
		for each (var item2:XML in c1.inside){
			var children2:Array = toArray(item2.children());
			children2.sort();
			item2.setChildren(new XMLList());
			for each (var c2:XML in children2){
				item2.appendChild(c2);
			}
		}
		item1.appendChild(c1);
	}
	
	
	for each (var c2:XML in children2){
		for each (var item3:XML in c2.deep){
                  var children3:Array = toArray(item3.children());
                  children3.sort();
                  item3.setChildren(new XMLList());
                  for each (var c3:XML in children3){
                        item3.appendChild(c3);
                  }
            }
		item1.appendChild(c2);
	}
	
	
}

trace(item1);

Open in new window

0
 
LVL 29

Accepted Solution

by:
dgofman earned 500 total points
Comment Utility
No, loops must be nested I attached code bellow, but you may use two level sorting if you want just code nested elements under depth
By modifying in my previous code
 
for each (var item2:XML in c1.inside){
to
for each (var item2:XML in c1.inside.deep){


Code bellow three levels sortings

for each (var item1:XML in source){
	var children1:Array = toArray(item1.children());
	children1.sortOn("string");
	item1.setChildren(new XMLList());
	for each (var c1:XML in children1){
		for each (var item2:XML in c1.inside){
			var children2:Array = toArray(item2.children());
			children2.sort();
			item2.setChildren(new XMLList());
			for each (var c2:XML in children2){
				for each (var item3:XML in c2.deep){
					var children3:Array = toArray(item3.children());
					children3.sort();
					item3.setChildren(new XMLList());
					for each (var c3:XML in children3){
						item3.appendChild(c3);
					}
				}
				item2.appendChild(c2);
			}
		}
		item1.appendChild(c1);
	}
}

Open in new window

0
 

Author Comment

by:Angha110
Comment Utility
Thank you so much.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 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.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

771 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