Copy link to clipboard
Copied
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).
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;
}
}
Copy link to clipboard
Copied
are you alphabetizing based on textfield name? textfield text? something else?
Copy link to clipboard
Copied
I'm alphabetizing by textfield text.
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
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!)
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
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!!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now