Skip to content
Advertisement

Sorting alphabetically by Array Value in Multidimensional Indexed Array After Custom Sort (usort) / Sort Array by Two Values

Original Array:

JavaScript

Applied usort($terms, "mysort"); via the following function(s)

JavaScript

This successfully ordered the array by the $sortOrder array in the getSortOrder function (base material first, and then color)

JavaScript

Now I am trying to sort this new sorted array by name while keeping the previously applied sort order (base material first and then color).

Expected Output:

JavaScript

Normally I could apply a usort function like such:

JavaScript

But this is of course messing up the output of the original description sort.

How can I sort first by description as in the functions above, and then proceed to sort by name while keeping the description sort in tact?

Advertisement

Answer

You could use array_multisort to sort your original data in one go to the expected output (sort by description first, then name) like so

JavaScript

$data will now be sorted by description first and name second.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement