0

Is there a way to assign a "priority score" to an array in PHP? Then sort based on that priority. Given my input:

Array(
  [princeton] => Princeton
  [stanford] => Stanford
  [yale] => Yale
  [mit] => MIT
  [harvard] => Harvard 
)

I would like the output to always have Harvard and Yale at the top, the rest alphabetically sorted:

Array(
  [harvard] => Harvard
  [yale] => Yale
  [mit] => MIT
  [princeton] => Princeton
  [stanford] => Stanford    
)

I checked out asort() which does the alphabetical and uasort() which is custom-defined, but I'm stuck on how I can "assign a priority". Any help would be appreciated!

4
  • Where do the priorities come from? Commented Jan 29, 2014 at 0:08
  • I would handset them, there's only a couple, but I'm stuck at how to do the assigning. Commented Jan 29, 2014 at 0:09
  • removing and adding to top after sorting alphabetly seems trivial Commented Jan 29, 2014 at 0:11
  • 1
    With uasort, you would define the sorting between 2 items. If neither is Harvard or Yale, do a strcmp, if one of them is, make it sort before the other, if both are, return 0 (unless you want harvard to sort before yale always, in which case, you could once again return a strcmp when both are harvard or yale). Commented Jan 29, 2014 at 0:11

3 Answers 3

2

uasort will sort your array by your own comparison function. I've made the function place either Harvard or Yale first (the ordering of those two will be uncertain since you haven't specified) and sort the rest alphabetically.

function cmp($a, $b) {
    if ($a == 'Yale' || $a == 'Harvard') {
        return -1;
    } else if ($b == 'Yale' || $b == 'Harvard') {
        return 1;
    } else return strcmp($a, $b);
}

uasort($array, 'cmp');
Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps you were overthinking this.

  1. sort by key
  2. replace your short priority array with the alphabetized array

Code: (Demo)

$ivyLeaguers = [
  'princeton' => 'Princeton',
  'cornell' => 'Cornell',
  'stanford' => 'Stanford',
  'yale' => 'Yale',
  'mit' => 'MIT',
  'harvard' => 'Harvard'
];

ksort($ivyLeaguers);
var_export(
    array_replace(['harvard' => null, 'yale' => null], $ivyLeaguers)
);

Output:

array (
  'harvard' => 'Harvard',
  'yale' => 'Yale',
  'cornell' => 'Cornell',
  'mit' => 'MIT',
  'princeton' => 'Princeton',
  'stanford' => 'Stanford',
)

The null values are overwritten but their position is respected. This technique pulls the priority schools to the top of the list based on the matching keys.

Comments

0

Assuming you have something like

$priotities=array(
  [harvard] => 1
  [yale] => 2
  [mit] => 3
  [princeton] => 4
  [stanford] => 5
)

you could

$final=$priorites;
sort(final);
foreach ($final as $k=>$v) 
  $final[$k]=$universities[$k];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.