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

alphabetizing xml filled dynamic textfields

New Here ,
Mar 12, 2013 Mar 12, 2013

So I am looking to alphabetize dynamically created textfield created using a for each loop and XML.

How would I go about this, I searched around and there isn't much about this, and now I feel like I haven't the slightest clue. The one post I found is missing it's own created Utilities class (I believe):

http://help.mfazio.com/?p=75&cpage=1#comment-39.

So where do I even begin?

Thanks for any help in advance! I'm still learning so the more you explain it, the more I learn (Thanks).

TOPICS
ActionScript
2.1K
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

Community Expert , Mar 12, 2013 Mar 12, 2013

add your textfields to an array and sort it using a custom sort function:

// after text is assigned:

var tfA:Array=[array of textfields];

tfA.sort(sortByText);

function sortByText(tf1:TextField,tf2:TextField):int{

if(tf1.text<tf2.text){

return -1;

} else if(tf1.text>tf2.text){

return 1;

} else {

return 0;

}

}

Translate
Community Expert ,
Mar 12, 2013 Mar 12, 2013

are you alphabetizing based on textfield name?  textfield text?  something else?

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 12, 2013 Mar 12, 2013

I'm alphabetizing by textfield text.

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
Community Expert ,
Mar 12, 2013 Mar 12, 2013

add your textfields to an array and sort it using a custom sort function:

// after text is assigned:

var tfA:Array=[array of textfields];

tfA.sort(sortByText);

function sortByText(tf1:TextField,tf2:TextField):int{

if(tf1.text<tf2.text){

return -1;

} else if(tf1.text>tf2.text){

return 1;

} else {

return 0;

}

}

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 12, 2013 Mar 12, 2013

I have to sort over 100+ textfields, will this way have to be done one by one?

I guess I'm just not fully understanding (again, ELI5...still learning, 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
Community Expert ,
Mar 12, 2013 Mar 12, 2013

i gave you all the code you need to sort all 100+ textfields.  the thing not done for you is adding the textfields to tfA but, if you used a pattern to name those textfields (eg, tf1, tf2,...), even that can be done via code.

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 13, 2013 Mar 13, 2013

Sorry I was away. I did not name the textfields 😕 they are sprites using a for each loop. I have to do everything with these textfields via code because of 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
Community Expert ,
Mar 13, 2013 Mar 13, 2013

then this should take almost no extra work, at all:

var tfA:Array=[];

//push your textfields into tfA in your for-loop where they are created.  after text is assigned and you want to sort the textfields, call sortTF().

function sortTF():void{

tfA.sort(sortByText);

}

function sortByText(tf1:TextField,tf2:TextField):int{

if(tf1.text<tf2.text){

return -1;

} else if(tf1.text>tf2.text){

return 1;

} else {

return 0;

}

}

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 14, 2013 Mar 14, 2013

Nice! Just a question, where you put

if(tf1.text<tf2.text){

return -1;

} else if(tf1.text>tf2.text){

return 1;

} else {

return 0;

will I have to write

if(tf1.text<tf2.text<tf3.text<tf4.text<tf5.text.......)

and so on?

I'm kind of just curious how this works. I just want to learn as much as possible. Thanks for the help!

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
Community Expert ,
Mar 14, 2013 Mar 14, 2013

no.

just copy and paste the code in message 7 and use the push method to add textfields to tfA in the for-loop where you are creating your sprites.

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 14, 2013 Mar 14, 2013

Thanks for helping me out, seriously!

Unfortunately, after testing, it didnt alphabetize, but it also produced no errors. Does it make any difference that these textfields are also placed into a movie clip?

I assume in this case no, since where I push it into the array, I can still call them individually. This is a time where I wish I had an error!

It seems that when I added a trace to sortByText, that is is not firing either.

Where I push them into the array:

                    public var tfA:Array=[];

                    public function ParseList(xmldata:XML):void

                    {          

                    var nameList:XMLList = xmldata.Searchtext.Directory;

                    directoryScreen.directoryHolder.addChild(textContainer);

                    directoryScreen.directoryHolder.mask = directoryScreen.mask_mc;

                    for each (var nameElement:XML in nameList){

                    format.font = "Arial MT";

                    format.size = 24;

                    format.color = 0x006699;

                    format.align = "left";

                    var currTextField:TextField = new TextField();

                    currTextField.defaultTextFormat = format;

                    currTextField.appendText(nameElement.text() + "\n");

                    currTextField.y= -750+ textContainer.height;

                    currTextField.height = 32;

                    currTextField.width = 280;

                    currTextField.border = false;

                    currTextField.x= -200;

                    currTextField.selectable = false;

                    tfA.push(currTextField);                    

                    textContainer.addChild(currTextField);

                    trace(nameElement.text());

                    textContainer.addEventListener(MouseEvent.CLICK, btnListener);

                    sortTF();

                      }

                    }

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
Community Expert ,
Mar 14, 2013 Mar 14, 2013

what are you doing with the sorted textfields?  ie, after sortTF() is called i don't see you doing anything with tfA which, after sortTF is called, contains the sorted textfields.

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 14, 2013 Mar 14, 2013

They are inside of a general textfield called textContainer (so that they will go one sprited textfield under the other and keep together), and then inside of a movieclip called directoryHolder (so I can scroll them) which is inside of another movieclip called directoryScreen (because this tweens on and off the stage with a button).

This is a directory, where when you click on one of the sprited textfields (a name) it lights up a section on a map. The textfields are filled with XML info.

So my goal is to have it alphabetize the sprited textfields by code, so new additions to my XML dont ruin a pattern.

Oh sorry, so yes directly after that is this:

     function sortTF():void{

           tfA.sort(sortByText);

           trace("sorted");

        }

         function sortByText(tf1:TextField,tf2:TextField):int{

            if(tf1.text<tf2.text){

               return -1;

            } else if(tf1.text>tf2.text){

               return 1;

            } else {

               return 0;

            }

          trace("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
Community Expert ,
Mar 14, 2013 Mar 14, 2013

you're not encoding what you described.  use:

     function sortTF():void{

           tfA.sort(sortByText);

var nextY:Number;

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

tfA.y=nextY;

nextY+=tfA.height;

}

        }

         function sortByText(tf1:TextField,tf2:TextField):int{

            if(tf1.text<tf2.text){

               return -1;

            } else if(tf1.text>tf2.text){

               return 1;

            } else {

               return 0;

            }

          trace("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
New Here ,
Mar 14, 2013 Mar 14, 2013

When I implement your changes, all the text is ontop of each other on the same line, and the output remains unalphabetized.

My text originally comes one after the other using

                    currTextField.y= -750+ textContainer.height;

                    currTextField.height = 32;

I assume thats what your

                    var nextY:Number;

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

                     tfA.y=nextY;

                     nextY+=tfA.height;

is also trying to implement?

Thanks for sticking with me here.


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
Community Expert ,
Mar 14, 2013 Mar 14, 2013

nextY should be initialized:

     function sortTF():void{

           tfA.sort(sortByText);

var nextY:Number=0

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

tfA.y=nextY;

nextY+=tfA.height;

}

        }

         function sortByText(tf1:TextField,tf2:TextField):int{

            if(tf1.text<tf2.text){

               return -1;

            } else if(tf1.text>tf2.text){

               return 1;

            } else {

               return 0;

            }

          trace("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
New Here ,
Mar 14, 2013 Mar 14, 2013

It works!!

I'm now having a glitch with my scroll bar, but I think some tinkering may help.

Thank you!! Soo much!!

Figured out my glitch, I had to set the var nextY to my original y start point.

Again not enough 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
Community Expert ,
Mar 14, 2013 Mar 14, 2013
LATEST

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