12 Replies Latest reply on Aug 19, 2006 3:39 AM by Newsgroup_User

# SortOn

Hello,

figured I get an ouput of
// my_array[0].price = 100
// my_array[1].price = 100
// my_array[2].price = 200
// my_array[3].price = 200

[object Object],[object Object],[object Object],[object Object]

• ###### 1. Re: SortOn
Darn,

figured it out.

Thanks,
sky
• ###### 2. Re: SortOn
Wait,

It sorts by price but how do you resort to get it to be

0 //(100), 2 //(100), 1 //(200), 3 //(200)

it rearranges the price to be 100,100,200,200 and outputs
0 //(100) ,1 //(100) , 2 //(200),3 //(200),

thanks,
sky
• ###### 3. Re: SortOn
If you're using Array.RETURNINDEXEDARRAY, you will get an array that holds the indexes of the original array, like 0,2,1,3. I don't understand what output you refer to. Do you want to change the 'id' properties to reflect the sorted index?
• ###### 4. Re: SortOn
Here's the deal.
I want to sort an array

var myArray:Array= new Array();

myArray.push({price:100, picture:"100.gif", description:"my stuff", id:"1"});
myArray.push({price:200, picture:"300.gif", description:"his stuff", id:"2"});
myArray.push({price:100, picture:"200.gif", description:"her stuff", id:"3"});
myArray.push({price:200, picture:"400.gif", description:"our stuff", id:"4"});

I want to sort myArray so the price 100 is first or [0] indexed
then place all the 100's elements in order 0-1 into respective text fieldsand

I figured I'd have to sort myArray to get the 100's on top
then sort again to get my 100's indexed starting at [0].

If I sort and want the 200's in myArray on top
I want to be able to do that too.

I think i'm so far lost it's not even funny. I'm frustrated. I don't think I can even explain what I want anymore. (laugh) I'm now crazier for opening up this software.

Ultimate goal:

To be able to pull elements (price, id, description, an image) from an array
place each element into a field or image loader in an .swf.

PLUS, for every additional number like the second, third, fourth 100 on the list put the elements " price" and "image" into another loader component that is inside a nother loader component.

confused? so am I.

I'll see if I can figure out a better way off doing this to post again.
• ###### 5. Re: SortOn
Ok, why not extract the items you want, instead of sorting the array? Use a function that gets the value you're looking for, loops through the array and returns an array that contains all the matching entries. Then you could sort that array if needed, and use the entries in the display.
I wanted to write some example code and ended up in a prototype function... It's nice to use though. :)
Put the function definition somewhere in your movie, maybe frame 1, just before the frames where you want to use it. Then use something like

priceX = myArray.getItems("price",300);

and you have all the matching entries.

• ###### 6. SortOn
Just came across this site: http://proto.layer51.com/d.aspx?f=99.

So if your arrays are rather big, it may be faster to use a while loop when looping through the array. The prototype function could also use it to be faster.

• ###### 7. Re: SortOn
prototype, ok..? looking at that code it seems interesting and reading the link you provided.

don't really grasp it.

The adobe tutorials are hard for me to comprehend is there another tutorial out online that might be more
"for dummies" style?

Could you comment out the code you wrote line by line? that might help me understand and use it
to create my own for my situation.

I searched for prototype in the help and it brings up "Classes". Should I familiarize myself with "Classes"
before delving into prototype or can I just use the prototype for my array.

any additional direction would be much appreciated.

thanks,

sky

• ###### 8. Re: SortOn
Well, if you deal with Actionscript, it's sure a good thing to be familiar with classes. AS is object-orientated, so classes are a basic part of the language. I'd recommend you to read about OOP (object orientated programming), to understand the classes and objects model. A good quick introduction is here: http://www.quip.net/blog/2006/flash/actionscript-20/ojects-building-blocks (/waves to David), and more detailed tutorials here: Texhttp://www.kirupa.com/developer/flash/index.htm#Advanced_/_ActionScriptt. There are several tutorials on OOP, and also a tutorial on Arrays, quite a lot to read ;).

The prototype method is a way to attach a custom function to an existing class. Every class object has this property, so when you attach a function to the prototype of the class Array, all objects of that class 'know' that function and can execute it like any other member function of that class. Inside the function, the 'this' keyword refers to the actual array object that is executing the function.
The method could also be a 'standalone' function, it would just need the array reference as additional parameter and replace all 'this' inside with the reference.

It's quite compact code, so I'll try to comment:
var res:Array = new Array();
var i = this.length;
This creates a new Array and gets the length of the array that is executing the function ('this'). It then loops through the array from the top, using the length var.
while (i--)
This is equal to while (i == true). Values that are not true are false, undefined and 0. Every other value will always be true, so the while loop stops when i == 0. The substraction (--) takes place after the value of i is evaluated, so inside the loop, the value is actually 1 less. It's the loop condition and the change of the loop variable in one line, the same as
while (i > 0){
i--;
...
}
Inside the loop, it tests every entry for the given property and value:
if (this[ i ][prop] == val)
is the same as if (myArray[ i ].propvalue == thevalue). When the values match, it adds the whole item to the result array.
When the loop has finished, that array is returned and contains references to all the items that fit, or is empty when no item matched. The original array is left as it was before.

Hope this helps :).
cheers
• ###### 9. Re: SortOn

What I've learned so far seems to be how to link to an external AS file.

I think I'm going to work on what you commented out for me now.

thank you for commenting out the code. It will definetly help.

sky
• ###### 10. SortOn
again,thanks,
• ###### 11. Re: SortOn
You're welcome. Inheritance and subclasses are quite advanced stuff, I think you don't need to know all this when you just want to get into OOP. If you know what classes and objects are about, that's fine, this should help a lot.

greets,
blemmo
• ###### 12. Re: SortOn
You're welcome. Inheritance and subclasses are quite advanced stuff, I think
you don't need to know all this when you just want to get into OOP. If you know
what classes and objects are about, that's fine, this should help a lot.

greets,
blemmo