Author Topic: custom sort function PHP  (Read 769 times)

0 Members and 1 Guest are viewing this topic.

Offline theKit

  • Devotee
  • **
  • Posts: 178
    • View Profile
custom sort function PHP
« 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'

Offline Lanny

  • Zealot
  • ****
  • Posts: 1,123
    • View Profile
Re: custom sort function PHP
« Reply #1 on: October 23, 2014, 11:21:26 pm »
This should do it.

Code: [Select]
$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.
« Last Edit: October 24, 2014, 12:01:24 am by Lanny »

Offline aldra

  • Arch Disciple
  • ***
  • Posts: 623
  • albrecht drais
    • View Profile
Re: custom sort function PHP
« Reply #2 on: October 23, 2014, 11:48:42 pm »
I would use foreach there, but pretty much the same code otherwise...


you missed a $ in front of some of the i's though

Offline Lanny

  • Zealot
  • ****
  • Posts: 1,123
    • View Profile
Re: custom sort function PHP
« Reply #3 on: October 24, 2014, 12:04:21 am »
Stupid dollar signs :mad:

Offline RustyShackleford

  • Devotee
  • **
  • Posts: 213
    • View Profile
Re: custom sort function PHP
« Reply #4 on: October 24, 2014, 12:08:58 am »
To get the ops output you have to change it to this
Code: [Select]
$sorted[$i] = $a2[$a1[$i]];

Offline aldra

  • Arch Disciple
  • ***
  • Posts: 623
  • albrecht drais
    • View Profile
Re: custom sort function PHP
« Reply #5 on: October 24, 2014, 12:32:20 am »
Code: [Select]

$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

Offline RustyShackleford

  • Devotee
  • **
  • Posts: 213
    • View Profile
Re: custom sort function PHP
« Reply #6 on: October 24, 2014, 01:07:09 am »
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);

Offline aldra

  • Arch Disciple
  • ***
  • Posts: 623
  • albrecht drais
    • View Profile
Re: custom sort function PHP
« Reply #7 on: October 24, 2014, 01:25:26 am »
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.

Offline theKit

  • Devotee
  • **
  • Posts: 178
    • View Profile
Re: custom sort function PHP
« Reply #8 on: October 25, 2014, 04:32:02 am »
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.

Offline aldra

  • Arch Disciple
  • ***
  • Posts: 623
  • albrecht drais
    • View Profile
Re: custom sort function PHP
« Reply #9 on: October 25, 2014, 06:12:54 am »
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