Search Unity

Sort an integer array and return the index values in order of size javascript

Discussion in 'Scripting' started by lasagafield, Nov 30, 2017.

  1. lasagafield

    lasagafield

    Joined:
    Nov 30, 2017
    Posts:
    5
    Hello and thank you for reading my post.
    I hope you can help
    So I am stumped with a problem with a racing game I am working on.
    I have a series of nodes across the track and an integer array with each player taking up a slot in it.
    Everytime you pass a node each players score goes up by one.
    So lets say I have an array like this:
    (5,3,7,4,5)

    And lets say I have 5 variables for each position.
    I would like the code to order each value by size and then assign each of the 5 variables with the index number of the array so it would look something like this:
    1st [3]
    2nd [1 or 5]
    3rd [5 or 1]
    4th [4]
    5th[2]

    I know sorting identical values would not give me a true answer but players quickly dispurse and have different values so I am not bothered how they would be sorted.
    How would you go about it?
    I have been stumped on this for days so sorry if my post isn't of very good quality.
    I hope you can help.
    Thank you.
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You might want to consider switching to C# :) Unrelated to your main question, but just saying..
     
  4. lasagafield

    lasagafield

    Joined:
    Nov 30, 2017
    Posts:
    5
    "You might want to consider switching to C#"
    Too late for that i'm afraid. It's quite a big project and backend wise it's almost complete.
    If I use array.sort will it swap the values round in different index's or reorder them by index?
    eg.
    index 12345 - 12 13 53 92 8 value
    12345 - 8 12 13 53 92 sorted
    Because I need the value and it's matching index preserved so something like
    51234 - 8 12 13 53 92
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sounds like you could accomplish that with 2 lists and your own sorting, maybe?
    By 2 lists, I mean the original (unsorted), and a new list/array of the same length, but in your example, it would read:
    Indexes: 0, 1,2,3,4 = (values) 5,1,2,3,4 (which would be the index into the orig. list).
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You could probably just use a while loop. javascript isn't that much different. You should be able to figure it out.
    Code (csharp):
    1.  
    2. int[] values= new int[5];
    3. int[] indexByValue = new int[5];
    4. var currentLowIndex;
    5. var indexCounter = 0;
    6. while(indexCounter < values.Length){
    7.   for(int i = 0;i<values.Length;i++{
    8.   if(i==0)
    9.    currentLowIndex = values[i];
    10.   else if (values[i] <values[ currentLowIndex])
    11.      currentLowIndex = i;
    12.  if(i == values.Length -1){
    13.     indexByValue[indexCounter] = currentLowIndex;
    14.    indexCounter++;
    15.    values[currentLowIndex] = 1000;
    16.   }
    17. }
    18. }
    19.  
    Totally untested, but I think something like that would work. Since it's such a few it shouldn't slow things down. If it doesn't work, at least it might help give you some ideas.
     
    Last edited: Nov 30, 2017
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That code doesn't look right, sorry :)
     
  8. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    OK. Like I said, it might give some ideas anyway. I'm terrible at laying down code. You shoud be able to run through, find the lowest index, and then set the value higher or delete it, and loop through the 5 numbers. A nested for loop would probably have been better. I should have just said it and not put down any code.:(
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You did fix the while loop portion, though , in an edit.. Cool =)

    I know what you mean, the OP has some ideas..
     
  10. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
  11. lasagafield

    lasagafield

    Joined:
    Nov 30, 2017
    Posts:
    5
    I have an idea.
    My sorting method bypasses the use of arrays and matches the corrisponding integer with an entry in the array with said value.
    What if I ditch the array all together and have multiple string values such as Player1_00 + score and then sort those values as 3 digit integers and have the code use a switch statment to see if the first 7 characters are "Player#" and set the corrisponding place.
     
  12. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    One of the reasons we use arrays is that they allow simple mathematical sorting. Many of us started out with var1 = something, var2 = something else. After long lists of variables, we find out how valuable arrays are for dealing with groups of variables because you can iterate through them and work with them as a group. You can have an array of structs that contain the player name and position, or time, for instance, and update it much more quickly. It's a matter of using arrays so they become second nature, and there is no time like the present.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Make a custom class that contains two ints, one for score and another for index number. Then make an array using that class. If the class was called ScoreIndex perhaps:

    [ScoreIndex(5, 1), ScoreIndex(3, 2), ScoreIndex(7, 3), ScoreIndex(4, 4), ScoreIndex(5, 5)]

    Then write a custom sorting function, and use that with System.Array.Sort on the array. The sort function is trivial since you're just comparing the score part of each entry. Since the index value is part of the same variable, it basically just comes along for the ride when the scores are sorted. Pun intended. See, racing game, along for the ride...OK, never mind. ;)

    --Eric
     
  14. lasagafield

    lasagafield

    Joined:
    Nov 30, 2017
    Posts:
    5
    Some stellar ideas in this thread right now!
    Thank you so much for your help everyone!