Forum Moderators: open
What I'm doing is creating a Flash interface for uploading multiple files to my site. It works fine but the files are not uploaded in any type of order except for one after another.
Here's what I have that is related to the array:
var fileRefList:FileReferenceList = new FileReferenceList();
... a browse button, etc ...
// create an array with the file names
var list:Array = fileRefList.fileList;
list.sort();
// create a reference to the files
var fileRef:FileReference;
status_txt.text += fileRef.name + '\n';
status_txt.vPosition = status_txt.maxVPosition;
What I want to do is sort the list array so that the files are uploaded in alphebetical order. It's cosmetic more than anything.
The problem is that the array isn't just an array of numbers, text or otherwise, it's an array of file reference objects and I'm not sure how to sort arrays of objects.
It's amazing how it helps to just write the question out sometimes...
Anyway, after posting my question I went to run some errands and the solution hit me before I left the driveway. I ended up writing my own function to do my sorting.
It's a simple bubble sort but it gets the job done. Here is the function:
// Array Sort Function
function SortArray(myArray)
{
var tempValue:String;
var done = "no";
var swap = "no";
var i;
var fileTemp1:FileReference;
var fileTemp2:FileReference;
while (done == "no") {
for (i = 0; i < (myArray.length - 1); i++) {
fileTemp1 = myArray[i];
fileTemp2 = myArray[i+1];
if (fileTemp1.name > fileTemp2.name) {
tempValue = myArray[i];
myArray[i] = myArray[i+1];
myArray[i+1] = tempValue;
swap = "yes";
}
}
if (swap == "no") {
done = "yes";
}
swap = "no";
}
return myArray;
}
So instead of just creating an array and assigning the fileList I create a temp array, assign the fileList then assign my original array to the sorted results of the temp array. This way I don't have to worry about updating any of my other code.
// create an array with the file names
var list1:Array = fileRefList.fileList;
var list:Array = SortArray(list1);
I'm sure I could clean up my code a bit by just sorting the original assignment of the data but because actionscript has been very touchy in the past I thought this was the easiest way.
I hope this helps someone else in the future!