playMaker

Author Topic: Sort GameObject Array according to Int Array Values[SOLVED]  (Read 5944 times)

bkups2003

  • Playmaker Newbie
  • *
  • Posts: 49
Sort GameObject Array according to Int Array Values[SOLVED]
« on: November 22, 2013, 04:17:20 AM »
Hello,

I have 2 arrays. Player Names (strings), and Player Scores (ints). How can i Sort BOTH arrays according to the ints in just the second array.

Here is an example:

(BEFORE)
Array 1          Array 2
Bob                50
Jack                10
Terry               25
Mike                5

I want the result:

(AFTER)
Array 1          Array 2
Bob                50
Terry               25
Jack                10
Mike                5

edit: Made it easier to understand. Changed example.
« Last Edit: September 14, 2015, 04:12:35 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Sort GameObject Array according to Int Array Values
« Reply #1 on: November 28, 2013, 05:34:17 AM »
Hi,

 For this you need a third temporary array that will sort the indexes the way you want.

 Typically, if you want to sort by score, duplicate the score array, sort it, and look up the scores in that sorted array and use this index, to then find the unsorted ones, and finally get the player name.

so:

(UNCHANGED)
Array 1          Array 2
Bob                50
Jack                10
Terry               25
Mike                5

( TEMP SORTED )
                   50
                   25
                   10
                    5

now, let's say you want to know the second best score:

1: get the second item in TEMP SORTED, it's 25
2: in "array 2", find the index of 25, it's 2 ( array indexes starts at 0)
3: in "array 1", get the player name at index it's "TERRY"

Does that make sense?

bye,

 Jean

bkups2003

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Sort GameObject Array according to Int Array Values
« Reply #2 on: December 02, 2013, 08:40:27 AM »
Oh yes, i Ended up doing something very similar except with a hash table and an array list.

Score int-> string
Added the score as the key to the Hash Table with the value as the Players Name
Keys -> Array (hash table keys action)
Sort
get 0/1/2 array value for 1st, 2nd, 3rd
hash table get that key.

What do you think? They are really doing the same thing, actually. Just The hash table has the keys which remove the need for the third array. Would their be any benefit over this way or over the way you mentioned in any way at all? Optimization or problems that may occur?

From what i can tell, there is a small problem apparent in both methods where if 2 players have the same score, it may find the same player twice...

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Sort GameObject Array according to Int Array Values
« Reply #3 on: December 03, 2013, 02:32:54 AM »
Hi,

Both solutions are ok.

to lift the duplicate problem, simply use your temporary as a pool you delete used items from. So everytime you pick an index, you delete it, and therefore if you have two duplicates, you will process them, because the first one is not there anymore.

bye,

 Jean

devnoir

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Sort GameObject Array according to Int Array Values
« Reply #4 on: August 25, 2015, 01:08:01 AM »
hello all,

i know this topic's old, but im a bit confused here.

once the arraylist has been sorted, which hashtable action is used to retrieve the keys?

im stuck on this part:

"hash table get that key."

Thanks!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Sort GameObject Array according to Int Array Values
« Reply #5 on: August 25, 2015, 01:31:04 AM »
Hi,
Check out tutorial 6 here or here with a sample file

devnoir

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Sort GameObject Array according to Int Array Values
« Reply #6 on: August 25, 2015, 11:13:15 AM »
@djaydino - thank you