The Sanctuary
Technology => Technophiliacs & Technophiles => Topic started by: theKit on October 23, 2014, 08:43:00 pm
-
I suck dick at righting algs. I need help writing a sort function that orders an array based on the values of another array.
Example:
Array that Defines the Order -
key[0] => [2]
key[1] => [1]
key[2] => [4]
key[3] => [0]
key[4] => [3]
I want the VALUES of the above array to order the following array:
pkey[0] => 'Sally'
pkey[1] => 'Jill'
pkey[2] => 'frank'
pkey[3] => 'greg'
pkey[4] => 'Korean_Jesus'
so the sort output equals this:
gkey[0] => pkey[2] => 'frank'
gkey[1]=> pkey[1] => 'jill'
gkey[2]=> pkey[4] => 'korean_jesus'
gkey[3]=> pkey[0] => 'sally'
gkey[4]=> pkey[3] => 'greg'
-
This should do it.
$a1 = array(2, 1, 4, 0, 3);
$a2 = array("Sally", "Jill", "frank", "greg", "Korean_Jesus");
$sorted = array();
for ($i=0; $i<count($a1); $i+=1) {
/* $a1[$i] is the index in the final array. Simple assignment. */
$sorted[$a1[$i]] = $a2[$i];
}
/* $sorted = array("greg", "Jill", "Sally", "Korean Jesus", "frank") */
It may be worth checking the arrays are the same length and that values of one correspond to indices of the other unless you have good reason to believe that will never happen.
-
I would use foreach there, but pretty much the same code otherwise...
you missed a $ in front of some of the i's though
-
Stupid dollar signs :mad:
-
To get the ops output you have to change it to this
$sorted[$i] = $a2[$a1[$i]];
-
$iSort =array(2,1,4,0,3);
$iNames =array('Sally','Jill','Frank','Greg','Korean_Jesus');
$iOut =array();
$y =0;
foreach($iSort as $x)
{
$iOut[$y]=$iNames[$x];
$y++;
}
unset $iNames;
might be a little more fault-tolerant
-
might be a little more fault-tolerant
What's the advantage of doing this as a foreach? Maybe I'm biased because I'm just more familiar with regular for loops, but this just means you have to declare an extra variable.
Also, not trying to nitpick, but I noticed cause I plugged the code in and the line with unset needs parenthesis - unset($iNames);
-
ahah, fair enough, I didn't actually test it, but that makes sense.
foreach will automatically calculate the constraints of the array so you don't need to use a static number or calculate the upper boundary yourself... as in if you were to have an array that has 10 items, foreach will iterate over each one... if you use for, you need to either iterate until 10 (which may be invalid if you use the function for multiple arrays of different sizes) or you need to calculate the number of items in the array on the fly. you could do it either way really, or even use while, but foreach is already optimised and easy to remember.
-
I appreciate the help and I've got the structure down now but I'm actually dealing with objects and it is fucking me over. Even though I pull the sorting numbers out of object, I get an error stating that I can't use output from an object as an iterator. Fucking OOP and it's object bullshit.
-
ah I understand now, was wondering about the references in the example...
I'm not a big fan of OO either; will check my aws code, was one of the things I couldn't avoid it for