How do I Sort Array?

0 favourites
From the Asset Store
Array Editor
$0.25 USD
95% off
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Hi Guys I have an array that contains football players name and position like this:

    Player name,Player Position

    Here is an example of one of the lines in the array:

    TestName,DF

    etc and I have 3 positions DF, MD and AT

    Each player will have one of those values

    How do I sort the array so that when the player chooses to sort the team into the formation for example

    5-3-2

    the first part 5 is DF

    the second part 3 is MD

    and the third part 2 is AT

    So it looks like this:

    DF,DF,DF,DF,MD,MD,MD,AT,AT

    How would I sort the array so that the 5 players with DF value are at the top then the 3 players with MD value are in the middle and the 2 players with AT value are at the bottom?

  • Here you go:

    https://www.dropbox.com/s/bseax8wgh8yc95t/Array2DSort.c3p?dl=0

    dop2000

    Thanks for the response -

    How would I do this is if I had 18 players in the array and I only wanted to play 10 so I only arrange 10? And the rest of the players are sub and not in the team. 11 players are on but the GK alway stays at the top. How would I do this?

    With the formations: -- there would be a button where these can be changed

    4-4-2

    4-3-3

    3-4-3

    5-3-2

  • You should've given all these additional details from the beginning.. Yeah, my method will not work.

    So if there are 18 players in the array, and you want to make the formation 5-3-2, what should happen? First 5 DF players move to the top of the array, and the reserve DF players (if there are any) should stay in the end of the array? Or you need to remove all reserve players, only leaving 10 people in the array?

    I think it would be easier to copy records to another array.

    Or even easier if you had sprite instances instead of the array. 18 instances, you pick all DF, mark first 5 of them as selected for the game. Then pick all MD players, select 3 of them, then pick AT player, select 2 of them.

  • You should've given all these additional details from the beginning.. Yeah, my method will not work.

    So if there are 18 players in the array, and you want to make the formation 5-3-2, what should happen? First 5 DF players move to the top of the array, and the reserve DF players (if there are any) should stay in the end of the array? Or you need to remove all reserve players, only leaving 10 people in the array?

    I think it would be easier to copy records to another array.

    Or even easier if you had sprite instances instead of the array. 18 instances, you pick all DF, mark first 5 of them as selected for the game. Then pick all MD players, select 3 of them, then pick AT player, select 2 of them.

    dop2000

    here is a demo a colleague provided:

    Fill out the first name surname and email with random stuff it doesn't matter

    Then go to the squad screen

    and you should see the formations box

    This is what I'm trying to recreate. With the formation changes. Does this explain?

  • Ok, I tried it. But it doesn't sort the array of players, it simply assigns different positions to the first 11 players. So player #4 for example, can be DF in formation 5-3-2. But the same player #4 will become MD in formation 3-4-3. The remaining 7 players are not used at all.

    If this is what you want, then it's very easy. For formation 5-3-2:

    Repeat 10 times: 
    Array set (loopindex<5?"DF":loopindex<8?"MD":"AT") at x=loopindex, y=1
    
  • Ok, I tried it. But it doesn't sort the array of players, it simply assigns different positions to the first 11 players. So player #4 for example, can be DF in formation 5-3-2. But the same player #4 will become MD in formation 3-4-3. The remaining 7 players are not used at all.

    If this is what you want, then it's very easy. For formation 5-3-2:

    > Repeat 10 times: 
    Array set (loopindex<5?"DF":loopindex<8?"MD":"AT") at x=loopindex, y=1
    

    dop2000

    Thank you

    - how would I do this for the other formations?

  • Just change the expression. For 3-4-3 it will be (loopindex<3?"DF":loopindex<7?"MD":"AT")

    Or you can do it the long way with several sub-events:

    If loopindex<3 : Array set... "DF"
    Else If loopindex<7 : Array set... "MD"
    Else : Array set... "MD"
    
  • dop2000

    The player names get lost in the transfer aswell how do I fix this:

    Also how did you know for the 3-4-3 its:

    (loopindex<3?"DF":loopindex<7?"MD":"AT")

    How do I work that out for the others?

    Also the player with GK is supposed to be at the top except it puts the player with the DF at the top how can I start the DF players from the 2nd position?

  • dop2000

    heres the link to the .c3p file I have

    dropbox.com/s/1rkpq15y5su094z/New%20project%20%281%29.c3p

    I changed Y to 1 to make it transfer the player names - But I still can't get as player with GK staying at the top whilst the formation is changed

  • You are probably overwriting the name, check your code.

    3-4-3 is first 3 players are "DF", next 4 players are "MD", and next 3 players are "AT", right?

    So if you are looping from index 0 to 9, loopindex<3 are "DF". You can use a short version of if-then-else for this, it's called ternary operator:

    loopindex<3 ? "DF" : "something else"

    And then nest another ternary operator inside:

    (loopindex<3 ? "DF" : (loopindex<7 ? "MD" : "AT"))

  • You are probably overwriting the name, check your code.

    3-4-3 is first 3 players are "DF", next 4 players are "MD", and next 3 players are "AT", right?

    So if you are looping from index 0 to 9, loopindex<3 are "DF". You can use a short version of if-then-else for this, it's called ternary operator:

    loopindex<3 ? "DF" : "something else"

    And then nest another ternary operator inside:

    (loopindex<3 ? "DF" : (loopindex<7 ? "MD" : "AT"))

    dop2000

    Sorry I am still confused what would it be for the others and how would I keep the player with GK position at the top?

  • Ok, forget about ternary operator. With the goalkeeper, 3-4-3 formation:

    For "n"=0 to 10
    
     If loopindex=0 : Array set "GK" at X=loopindex, Y=..
    
     Else If loopindex<4 : Array set "DF" at X=loopindex, Y=..
    
     Else If loopindex<8 : Array set "MD" at X=loopindex, Y=..
    
     Else : Array set "AT" at X=loopindex, Y=..
    
    
  • Ok, forget about ternary operator. With the goalkeeper, 3-4-3 formation:

    > For "n"=0 to 10
    
    If loopindex=0 : Array set "GK" at X=loopindex, Y=..
    
    Else If loopindex<4 : Array set "DF" at X=loopindex, Y=..
    
    Else If loopindex<8 : Array set "MD" at X=loopindex, Y=..
    
    Else : Array set "AT" at X=loopindex, Y=..
    
    

    dop2000

    I still cant get this working Please could you provide a .c3p file

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)