1

I have an array, crewPositionsAC that contains a list of position abreviatations - EP, PR, DR, WR, and so on. These positions are read in through an XML file each time my flex application loads. Also being populated from an XML is a project. Within a project, there are positions (a student assigned to a type of position listed within crewPositionsAC). These positions are not necessarily in the correct hierarchy order dictated by crewPositionsAC. I have all the positions within an ArrayCollection (positionsAC) with the following structure:

positionsAC (arrayCollection)
    [0] = Array
        [0] = startOffset
        [1] = numDays
        [2] = role
        [3] = studentID
        [4] = conflict
        [5] = studentType
        [6] = showInPrinciple
        [7] = revisionNumber
    *continue until all positions are listed*

My question is this, how can I reorder positionsAC to ensure that the "role" pieces of each array are in the correct order (as dictated by crewPositionsAC)? I've tried a couple different for loops, but nothing even came close.

Edit

So, there are several projects, within each project there are several positions (usually 16 or 17, but there is no set number.

Within a project, there is a variable called positionsAC that has the following structure:

positionsAC:
    [0] (array)
        [0] = startOffset
        [1] = numDays
        [2] = role
        [3] = studentID
        [4] = conflict
        [5] = studentType
        [6] = showInPrinciple
        [7] = revisionNumber
    *continue until all positions are listed*

Then, the user can click a button to add another position. When the "Add Crew Member" button is pressed, the user is presented with a list of possible positions to add. Currently, I simply add another array to positionsAC. This results in the recently added crew member to placed on the bottom of the list. I need to take positionsAC and reorder it based on it's [2] item (role) based on the hierarchy defined in the crewPositionsAC. crewPositionsAC has the following structure:

crewPositionsAC:
    [0] = EP
    [1] = PR
    [2] = DR
    [3] = WR
    * continue until all possible position types are listed

Hope that helps a little bit.

Mattias
  • 3,907
  • 4
  • 28
  • 50
Brds
  • 1,035
  • 3
  • 17
  • 37
  • Are you saying that students are assigned a position abbreviation (e.g. EP or PR or WR or ... ) and that there is only one student per position and the order in which the position abbreviations appear is the order you'd like to present the students in? So that, for example, if abbreviation WR was first in your list of abbreviations, the students with position=WR would be listed first in your students list? – Tim Sep 16 '11 at 16:11
  • Yes, that's exactly what i'm saying. =) I've added a little bit more info to the original post to help make things more clear. – Brds Sep 16 '11 at 16:18

2 Answers2

1

Try these loops:

for (var i:int = 0; i < crewPositionsAC.length; i++)
{
    var sourcePosition:Object = crewPositionsAC.getItemAt(i);
    var actualPosition:Object = findActualPositionByRole(sourcePosition);
    positionsAC.setItemAt(actualPosition, i);
}

function findActualPositionByRole(sourcePosition):Object
{
    for (var i:int = 0; i < positionsAC.length; i++)
    {
        var currentPosition:Object = positionsAC.getItemAt(i);
        if (currentPosition[2] == sourcePosition[2])
            return currentPosition;
    }

    throw new Error("There's no matching item with the specified role");
}

The above code should work under the assumptions that @Tim tries to verify.

Vladimir Tsvetkov
  • 2,983
  • 3
  • 26
  • 36
  • I get an error caused from the following line of code: "positionsAC.setItemAt(crewPositionsAC[item], i);" – Brds Sep 16 '11 at 16:06
  • 1
    @Brds Sorry, I accidentally hit the SAVE-button. I've edited the post. – Vladimir Tsvetkov Sep 16 '11 at 16:13
  • Error of "Property 2 not found on String and there is no default value." Coming from the line with "if (currentPosition[2] == sourcePosition[2])" – Brds Sep 16 '11 at 16:26
  • it should just be "sourcePosition"... not "sourcePosition[2]"... testing it out now – Brds Sep 16 '11 at 16:32
0

Ended up using the following as the base for achieving my goal:

for (var i:int = 0; i < parentDocument.crewPositionsAC.length; i++)
{
    for (var j:int = 0; j < positionsAC.length; j++)
    {
        if (positionsAC[j][2] == parentDocument.crewPositionsAC.getItemAt(i))
            tempAC.addItem(positionsAC[j] as Array);
    }
}

from there, I could use tempAC to get where I needed to go... thank vlad, you got me where I needed to be =)

Brds
  • 1,035
  • 3
  • 17
  • 37