0

I am currently working with arrays and sorting. I have worked with the usual traditional ways to organize an array. However, I also just found out about uasort() and how it allows sorting of an array with a user-defined comparison function and maintains index association. What would be the best way to apply uasort and sort the below array based on a user define order?

Desired order Principal, Teacher, Security, BusDriver, Maintanance:

  array(8) {
    [0]=>
    object(stdClass)#23019 (9) {
      ["id"]=>
      int(1599874)
      ["firstName"]=>
      string(3) "Jim"
      ["lastName"]=>
      string(6) "Turner"
      ["position"]=>
      string(9) "Principal"
    }
    [1]=>
    object(stdClass)#23034 (9) {
      ["id"]=>
      int(14398838)
      ["firstName"]=>
      string(7) "Michael"
      ["lastName"]=>
      string(6) "Watson"
      ["position"]=>
      string(9) "Principal"
    }
    [2]=>
    object(stdClass)#23038 (9) {
      ["id"]=>
      int(17398661)
      ["firstName"]=>
      string(8) "Jermaine"
      ["lastName"]=>
      string(4) "Shaw"
      ["position"]=>
      string(7) "Teacher"
    }
    [3]=>
    object(stdClass)#23003 (9) {
      ["id"]=>
      int(17398683)
      ["firstName"]=>
      string(5) "Maria"
      ["lastName"]=>
      string(6) "Garcia"
      ["position"]=>
      string(7) "Teacher"
    }
    [4]=>
    object(stdClass)#23020 (9) {
      ["id"]=>
      int(17398686)
      ["firstName"]=>
      string(5) "Jacob"
      ["lastName"]=>
      string(5) "Smith"
      ["position"]=>
      string(7) "Teacher"
    }
    [5]=>
    object(stdClass)#23021 (9) {
      ["id"]=>
      int(22398843)
      ["firstName"]=>
      string(4) "Alex"
      ["lastName"]=>
      string(4) "Hood"
      ["positionUsed"]=>
      string(8) "Security"
    }
    [6]=>
    object(stdClass)#23036 (9) {
      ["id"]=>
      int(13398706)
      ["firstName"]=>
      string(4) "Jose"
      ["lastName"]=>
      string(8) "Bautista"
      ["position"]=>
      string(9) "BusDriver"
    }
    [7]=>
    object(stdClass)#23029 (9) {
      ["id"]=>
      int(12398646)
      ["firstName"]=>
      string(6) "Travis"
      ["lastName"]=>
      string(9) "Dickerson"
      ["position"]=>
      string(11) "Maintenance"
    }
  }

Code:

$staff1 = [
    [
        'id'        => 1599874,
        'firstName' => 'Jim',
        'lastName'  => 'Turner',
        'position'  => 'Principal',
    ],
    [
        'id'        => 12398646,
        'firstName' => 'Travis',
        'lastName'  => 'Dickerson',
        'position'  => 'Maintanance',
    ],
    [
        'id'        => 17398683,
        'firstName' => 'Maria',
        'lastName'  => 'Garcia',
        'position'  => 'Teacher',
    ],
    [
        'id'        => 17398686,
        'firstName' => 'Jacob',
        'lastName'  => 'Smith',
        'position'  => 'Teacher',
    ],
    [
        'id'        => 13398706,
        'firstName' => 'Jose',
        'lastName'  => 'Bautista',
        'position'  => 'BusDriver',
    ],
    [
        'id'        => 14398838,
        'firstName' => 'Michael',
        'lastName'  => 'Watson',
        'position'  => 'Principal',
    ],
    [
        'id'        => 22398843,
        'firstName' => 'Alex',
        'lastName'  => 'Hood',
        'position'  => 'Security',
    ],
];

If array has smaller set of member still output the same order:

$staff2 = [
    [
        'id'        => 1599874,
        'firstName' => 'Jim',
        'lastName'  => 'Turner',
        'position'  => 'Principal',
    ],
    [
        'id'        => 12398646,
        'firstName' => 'Travis',
        'lastName'  => 'Dickerson',
        'position'  => 'Maintenance',
    ],
    [
        'id'        => 17398683,
        'firstName' => 'Maria',
        'lastName'  => 'Garcia',
        'position'  => 'Teacher',
    ],
    [
        'id'        => 13398706,
        'firstName' => 'Jose',
        'lastName'  => 'Bautista',
        'position'  => 'BusDriver',
    ],
    [
        'id'        => 22398843,
        'firstName' => 'Alex',
        'lastName'  => 'Hood',
        'position'  => 'Security',
    ],
];
MaryCoding
  • 624
  • 1
  • 9
  • 31
  • What do you mean by the title? Why should user-defined sorting take size into consideration? – Barmar Jun 23 '17 at 23:29
  • Have you considered mapping your `Principal, Teacher, Security, BusDriver, Maintanance` sort keys to 0, 1, 2, 3, 4 to make writing your compare function easier? And BTW, check spelling on Maintenance. – Chase Jun 23 '17 at 23:29
  • Why do you think the size of the array has anything to do with sorting it? – Barmar Jun 23 '17 at 23:32
  • @Chase, corrected the spelling typo. Oh good point. I have not considered mapping. New to mapping but will start looking into that. Thanks. – MaryCoding Jun 23 '17 at 23:34
  • @Barmar, I just realized it doesn't have anything to do with sorting. My apologies. Thanks. – MaryCoding Jun 23 '17 at 23:35

1 Answers1

0

Add a numeric value before every "position" value (Principal, Teacher, Security, BusDriver, Maintanance) and use usort or uasort using substr in your callbkack for first value or using preg_match fetching the preceding numbers if you, in future, could need use more than 10 prefixing numbers, then trim them.

It's a dirty fix, probably someone will give you a better solution, but this is the first one which has appeared on my mind.

xpeiro
  • 733
  • 5
  • 21