2

I went through all the similiar questions but couldn't find an answer....so here goes.

My current array, simplified:

[order] => Array
        (
            [0] => Array
                (
                    [strSupplier] => XYZ
                    (varying other fields)

                )

            [1] => Array
                (
                    [strSupplier] => XYZ
                    (varying other fields)
                )

            [2] => Array
                (
                    [strSupplier] => YYZ
                    (varying other fields)
                )

        )

Code:

                function custom_sort2($a,$b) {
                    return $a['strSupplier']>$b['strSupplier'];
                }  
                // Sort the multidimensional array            
                usort($tempOrderArray, "custom_sort2");

Currently, I am sorting on only the supplier, however, I need to ensure that the key is the second sort criteria, and I am not sure that it is.

Is there a way I can guarantee that it is sorted by strSupplier first, then key? If this is built into either the uasort or usort function, I apologize - I did not see it.

Cymbals
  • 1,164
  • 1
  • 13
  • 26
  • 1
    You could add another property to your second array dimension to store the key =/ but that could create maintainability issues. btw, your sort should return -1, 0, or 1, not true or false. – crush Jan 14 '13 at 18:19
  • It is just a temp array, so I could add the key as an element if needed. Would that then require a uasort? If you submit your answer below, I have a feeling it will end up being marked as correct unless something else comes along. Also, what should the return line look to fix the return value issue you mentioned? return ($a < $b) ? -1 : 1; ? – Cymbals Jan 14 '13 at 19:54
  • Confirmed - usort does not use the key as an additional criteria if the search fields are the same. Definitely not FIFO. – Cymbals Jan 14 '13 at 20:22

2 Answers2

1

Your array would like this this:

[order] => Array
        (
            [0] => Array
                (
                    [key] => 0,
                    [strSupplier] => 'XYZ',
                    //(varying other fields)

                )

            [1] => Array
                (
                    [key] = 1,
                    [strSupplier] => 'XYZ',
                    //(varying other fields)
                )

            [2] => Array
                (
                    [key] = 2,
                    [strSupplier] => 'YYZ',
                    //(varying other fields)
                )

        )

Then, when you sort:

function custom_sort2($a, $b) {
    $cmp = $cmpstr = strcmp($a['strSupplier'], $b['strSupplier']); //Compare the string
    $cmpkey = ($a['key'] == $b['key'] ? 0 : ($a['key'] > $b['key'] ? 1 : -1)); //Compare the key
    if ($cmpkey == 1)
        $cmp = ($cmpstr >= 0) ? 1 : 0;
    return $cmp; //If we are moving the element forward, then we need to check the key.
}
// Sort the multidimensional array
uasort($array, "custom_sort2");
crush
  • 16,713
  • 9
  • 59
  • 100
  • Wow... if I don't need to retain the array key afterward, using usort, is the rest code above still the same/needed? – Cymbals Jan 14 '13 at 21:09
  • Oh, I get what you are saying. Yeah, you can use usort if you like. The same comparison function would be used as above tho. – crush Jan 14 '13 at 21:15
0

You could try using ksort first, and then sort by supplier.

TurkeyFried
  • 132
  • 1
  • 1
  • 9
  • Forgive me - I need to sort by supplier, then the key. So how would one sort not undo the other? – Cymbals Jan 14 '13 at 19:48
  • The list would initially be sorted by key. When you sort that key-sorted list by Supplier, you will end up with a list where the most important order factor is the Supplier, followed by key. – TurkeyFried Jan 15 '13 at 14:11
  • That's what I though too, but after running tests...sorting by supplier ignored the key as a secondary criteria (FIFO did not apply). – Cymbals Jan 15 '13 at 14:16
  • That's weird, I did something pretty similar a few months ago. Just to verify, is ksort returning true and the array you give it sorted? – TurkeyFried Jan 15 '13 at 14:38
  • Argh, disregard my suggestion. I tried it myself and noticed that usort turns associative arrays into indexed arrays. – TurkeyFried Jan 15 '13 at 14:51