Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Sort Xml list

New Here ,
Mar 29, 2009 Mar 29, 2009
Hello,
I am trying to learn actionscript 3 for work and have a very specific project I need it for. I have been searching every available tutorial have bought a few books to read but I cannot find out how to do a few simple things.
The main thing I need to do right now is sort an xml list by the date of an event before I start picking which nodes to display. (<dt_event_start>) I need the function to be able to sort by date or by alpha for a different xml list I will be sorting later, not both at the same time, just one or the other. My code is sparse right now, all it does is pick events in a certain city and write them all in one big list to a text box on screen. Later I will have to separate out each part to make a header for each event and choose which nodes to display, so i need to sort the xml list before I start displaying the nodes in separate text boxes within a larger container repeated with a loop through the list.
included is one xml entry and the as3 code i have so far
Any help would be greatly appreciated and then I can go looking for the other code I need.
TOPICS
ActionScript
10.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Deleted User
Mar 30, 2009 Mar 30, 2009
You can't use sortXMLList as an event handler -- the function signature doesn't match. Try it like this:
Translate
LEGEND ,
Mar 29, 2009 Mar 29, 2009
Look at this thread:

http://board.flashkit.com/board/showthread.php?t=777257

It has all the info you need for sorting.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 29, 2009 Mar 29, 2009
You'll need to populate an array with the XMLList nodes, sort the array, and then toss them back into an XMLList. Something like this:
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2009 Mar 29, 2009
Wow, thanks for the fast replies, I'll work on these and then post my results.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2009 Mar 29, 2009
Ok, I've been messing with the code and I got the version you put up to run, but it came back with an error saying ArgumentError: Error #1063: Argument count mismatch on currentevents_fla::MainTimeline/sortXMLList(). Expected 2, got 1.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

and then I tried to get it to work with my external xml file and got this error and nothing displayed in the output window:
ArgumentError: Error #1063: Argument count mismatch on currenteventsdisplays_fla::MainTimeline/sortXMLList(). Expected 2, got 1.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
enclosed is my current version of the code trying to load the external xml with the same structure as before
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 30, 2009 Mar 30, 2009
You can't use sortXMLList as an event handler -- the function signature doesn't match. Try it like this:
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 30, 2009 Mar 30, 2009
You could also sort the original source xml. Something like this:
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2009 May 08, 2009

This thread looks like the same thing I am looking for but I don't see any of your examples. Is there a reason they are not visible? I could really use some help on this as well. Thank you in advance.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 09, 2009 May 09, 2009

This thread was "imported" from the old forums -- most, if not all, code seems to have disappeared. But here are the sort functions I posted:

function sortXML(source:XML, elementName:Object, fieldName:Object,

options:Object = null):XML

{

     // list of elements we're going to sort

     var list:XMLList=source.elements("*").(name()==elementName);

    

     // list of elements not included in the sort -

     // we place these back into the source node at the end

     var excludeList:XMLList=source.elements("*").(name()!=elementName);

    

     list= sortXMLList(list,fieldName,options);

     list += excludeList;

    

     source.setChildren(list);

     return source;

}

function sortXMLList(list:XMLList, fieldName:Object, options:Object =

null):XMLList

{

     var arr:Array = new Array();

     var ch:XML;

     for each(ch in list)

     {

           arr.push(ch);

     }

     var resultArr:Array = fieldName==null ?

           options==null ? arr.sort() :arr.sort(options)

           : arr.sortOn(fieldName, options);

    

     var result:XMLList = new XMLList();

     for(var i:int=0; i<resultArr.length; i++)

     {

           result += resultArr;

     }

     return result;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 05, 2010 Feb 05, 2010

thank you so much Raymond to repost your great functions !!!!!

you make my day

Cédric

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 01, 2011 Nov 01, 2011

100% usefull! thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 01, 2012 Dec 01, 2012
LATEST

Thanks Raymond, you saved my day

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 30, 2009 Mar 30, 2009
Thanks Raymond!!
That solution worked perfectly!
I wish compiler errors could tell you stuff like that, oh well,
thanks again!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 30, 2009 Mar 30, 2009
You're welcome
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines