0

Say I have list of males sorted by their scores and list of females sorted by their scores as well(from highest to lowest). Now I want to have a list of:

Highest score male, Highest score female, 2nd score male, 2nd score female, 3rd score male, 3rd score female, etc...

(Note that, event if highest score female lower than 2nd score male, it is not important.)

How could I do that with php ?

Update as requested:

Array called: $arr_sort[$score][$gender]

Input array 1:

$arr_sort[20][male],$arr_sort[18][male],$arr_sort[17][male],$arr_sort[10][male],$arr_sort[9][male],$arr_sort[5][male],$arr_sort[1][male]

Input array 2:

$arr_sort[15][female],$arr_sort[14][female],$arr_sort[13][female]

expected result:

$arr_sort[20][male],$arr_sort[15][female],$arr_sort[18][male],$arr_sort[14][female],$arr_sort[17][male],$arr_sort[13][female],$arr_sort[10][male],$arr_sort[9][male],$arr_sort[5][male],$arr_sort[1][male]

Different sizes doesn't matter, just mixing to whatever length of each one, (In short, we need to get a result so that if we remove the "male" array from the result we will get the original "female" array and vice versa. )

user1314404
  • 1,253
  • 3
  • 22
  • 51

1 Answers1

0

The input arrays are already sorted

If you know how to iterate through one of them, you know how to iterate through the other array as well. Iterate over both (alternating).

pseudo code:

Iterator m = new Iterator(array1);
Iterator f = new Iterator(array2);
List array3 = [];

while (m.hasNext() || f.hasNext() ) {
    if (male.hasNext())
        array3.add( m.next() );
    if (f.hasNext())
        array3.add( f.next() );
}

(EDIT) php example:

$array3 = [];
$mkeys = array_keys($array1);
$fkeys = array_keys($array2);
$nm = count($mkeys);
$nf = count($fkeys);

for ($i=0; ($i<$nm)||($i<$nf); ++$i) {
    if ($i<$nm) {
        //$array3[] = array($mkeys[$i], $array1[$mkeys[$i]]);
        $array3[$mkeys[$i]] = $array1[$mkeys[$i]];
    }
    if ($i<$nf) {
        //$array3[] = array($fkeys[$i], $array2[$fkeys[$i]]);
        $array3[$fkeys[$i]] = $array2[$fkeys[$i]];
    }
}

What the php code is doing is taking the key and the value and then adding it to an associative array, regardless of what that key and value are.

Since the arrays are already sorted, all that needs to happen is to alternate adding their elements to a merge array as per the specification in the question

Note that, event[sic] if highest score female lower than 2nd score male, it is not important

bgarcia
  • 154
  • 6
  • what happens if score is not integer but float number? Do you have a working code, actually, I do not really see where is the merging part? – user1314404 Apr 05 '16 at 19:48
  • @user1314404 What the php code is doing is taking the key and the value, wrapping it in an array, and then adding it to a position indexed array, regardless of what that key and value are. Since the arrays are already sorted, all that needs to happen is to alternate adding their elements to a merge array as per the specification in the question `Note that, event if highest score female lower than 2nd score male, it is not important` – bgarcia Apr 06 '16 at 13:55