Forums: Flash:

 

HELP with a hopefully quick Array sorting method

first
 

Storm HELP with a hopefully quick Array sorting method

AS3

I've got a list of job roles populating a custom drop down menu. I hold the jobs in an Array called jobArray. It works and the menu builds from it. No problem.

I'm having one small issue sorting it.

current:

			splicedArray = jobArray[i].splice(0,2);

jobArray.splice(0, 0, splicedArray);
// does the same as below -- MovieClip(root).jobArray.splice(MovieClip(root).jobArray.length-1, 1);
jobArray.pop();


The user's courses load based on their default job, but the list of courses changes when they choose courses for their other job roles. They click say drop down item 3, it needs to move to position [0] and move the others down one spot. This works, but my method leaves an undefined item in the number that was clicked.

Eg.
Sales Guy
Service Guy
Warranty Clerk

clicking [2] moves it to [0] no problem and the rest move down.

clicking [1] moves it to [0] and [1] becomes undefined. [2] stays as is.

Any wicked code helpers to help clarify what I am doing?

 

persist

I am not sure this answers it, but this is how i would do it.


var d = ["Sales Guy","Service Guy","Warranty Clerk"];


trace(d);

function newShift(array, indexfrom){
array.unshift(array[indexfrom]);
array.splice(indexfrom+1,1);
}


//clicking [2] moves it to [0]
newShift(d,2);
trace(d);


//clicking [1] moves it to [0]
newShift(d,1);
trace(d);


traces to:

Sales Guy,Service Guy,Warranty Clerk
Warranty Clerk,Sales Guy,Service Guy
Sales Guy,Warranty Clerk,Service Guy

 

Storm

Merci.

Now to read up on unshift versus pop.

 

persist

unshift does the same as push, but it puts the new element at the beginning of the array. I forget what it returns though. That may be different that what push returns.

 

Storm

thanks. I got it.

The difference for my brain versus yours is my thought is to cut out the selected one which leaves two pieces around it. Store those pieces, then put selected at 0, and add in the temp stored pieces after.

Your way of thinking take the first, put it at the front, and remove the empty space works much better. I just need to see how true programmers think versus my visual brain's thoughts.

Both will return the length as an uint. Unshift adds to the front, push adds to the back. I was trying to store two distinct pieces.

 

persist

You're thinking logically, using the tools you know. You're already thinking like a programmer.

A splice, under the covers, splits the array in two, and combines them back, just like how you were trying to do with verbose syntax.

There are a lot of methods out there on a lot of object types that do a lot of heavy lifting and you can't possibly know them all. In programming, a second and third set of eyeballs is priceless.

 
first
 

Forums: Flash: HELP with a hopefully quick Array sorting method

 
New Post
 
You must be logged in to post