0

I have the following data structure which needs to be sorted. First field to sort by is 'kategorie' and then 'order_by'. It's important that the keys of the objects remain unchanged, the urls get used in other parts of the code.

object(stdClass)#2 (31) {
      ["https://idp1.xxx/idp/shibboleth"]=>
      object(stdClass)#12 (4) {
        ["name"]=>
        string(19) "test"
        ["data"]=>
        string(39) "test"
        ["kategorie"]=>
        string(1) "2"
        ["order_by"]=>
        string(3) "200"
      }
      ["https://idp2.xxx/idp/shibboleth"]=>
      object(stdClass)#1 (4) {
        ["name"]=>
        string(52) "test"
        ["data"]=>
        string(60) "test"
        ["kategorie"]=>
        string(1) "3"
        ["order_by"]=>
        string(3) "110"
      } 
      ["https://idp3.xxx/idp/shibboleth"]=>
      object(stdClass)#1 (4) {
        ["name"]=>
        string(52) "test"
        ["data"]=>
        string(60) "test"
        ["kategorie"]=>
        string(1) "2"
        ["order_by"]=>
        string(3) "120"
      } ...

How it should look like:

object(stdClass)#2 (31) {
      ["https://idp3.xxx/idp/shibboleth"]=>
      object(stdClass)#1 (4) {
        ["name"]=>
        string(52) "test"
        ["data"]=>
        string(60) "test"
        ["kategorie"]=>
        string(1) "2"
        ["order_by"]=>
        string(3) "120"
      } 
      ["https://idp1.xxx/idp/shibboleth"]=>
      object(stdClass)#12 (4) {
        ["name"]=>
        string(19) "test"
        ["data"]=>
        string(39) "test"
        ["kategorie"]=>
        string(1) "2"
        ["order_by"]=>
        string(3) "200"
      }
      ["https://idp2.xxx/idp/shibboleth"]=>
      object(stdClass)#1 (4) {
        ["name"]=>
        string(52) "test"
        ["data"]=>
        string(60) "test"
        ["kategorie"]=>
        string(1) "3"
        ["order_by"]=>
        string(3) "110"
      } ...

Whats the easiest way to achive this? I just can't wrap my head around it...

Edit: usort doesn't work for stdClass Objects This array_multisort doesn't work either

  array_multisort(array_column($entries, 'kategorie'), SORT_ASC,
                  array_column($entries, 'order_by'),      SORT_ASC,
                  $entries);

Edit2: Typcasting to array and the using the above multisort was the answer.

  • Title is wrong, it's not supposed to be sorted by indices, it's supposed to be sorted by object datafileds 'order_by' and 'kategorie'. – Hans Müller May 30 '21 at 10:09
  • Does this answer your question? [Sort multidimensional array by multiple columns](https://stackoverflow.com/questions/3232965/sort-multidimensional-array-by-multiple-columns) – AymDev May 30 '21 at 10:09
  • usort doesn't work for stdClass Objects and the array column answer doesn't do anything to my data – Hans Müller May 30 '21 at 10:23
  • 1
    Well, and this one ? https://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields If your array comes from a `json_decode()` call, you pass `true` as 2nd parameter to get an associative array. – AymDev May 30 '21 at 10:28

1 Answers1

0

It doesn't work as expected, because you're sorting by strings, so "10" < "2". You can either convert these columns to numbers, or use SORT_NUMERIC flag:

array_multisort(
    array_column($entries, 'kategorie'),
    SORT_NUMERIC,
    array_column($entries, 'order_by'),
    SORT_NUMERIC,
    $entries
);
Robo Robok
  • 21,132
  • 17
  • 68
  • 126