Let's say I have this array:
$array = array(
array("id" => 7867867, "animal" => "Dog"),
array("id" => 3452342, "animal" => "Lion"),
array("id" => 1231233, "animal" => "Lion"),
array("id" => 5867867, "animal" => "Dog"),
array("id" => 1111111, "animal" => "Zeebra"),
array("id" => 2222222, "animal" => "Cat"),
array("id" => 3333333, "animal" => "Cat"),
array("id" => 4444444, "animal" => "Zeebra")
);
Now what I've been trying to do is use php sort functions to be able to sort it based on specific rules (not alphabetical)
The client wants this information sorted by "Lion first, Dog second, Zeebra third, Cat fourth".
Something like this:
$array = array(
array("id" => 3452342, "animal" => "Lion"),
array("id" => 1231233, "animal" => "Lion"),
array("id" => 7867867, "animal" => "Dog"),
array("id" => 5867867, "animal" => "Dog"),
array("id" => 4444444, "animal" => "Zeebra"),
array("id" => 1111111, "animal" => "Zeebra"),
array("id" => 2222222, "animal" => "Cat"),
array("id" => 3333333, "animal" => "Cat"),
);
The array would be sorted using the "animal" value and would be based on pre-determined rules.
I was trying to figure out php sort functions but I could only get those to work with sorting the arrays alphabetically or numerically.
What I've gotten to work is a block of if-statements and loops, and I would like to get rid of that slow code as soon as I can.
To define custom sort function, you need to compare first value with second value. If first value is greater than the second value, return -1. If first value is less than the second value, return 1 otherwise return 0. The above process will sort the data in descending order.
The syntax for the new SORT function is =SORT(array, [sort_index], [sort_order], [by_column]). The first argument identifies the array to be sorted. All the other arguments are optional. The second argument determines which column the array will be sorted by.
To sort the array of arrays, you need to specify based on which element you want to sort them. Here we compare the arrays by their second elements. Then the sort() function loops through the array of arrays and sorts it based on the magnitude of the second elements.
To sort an array of strings in Java, we can use Arrays. sort() function.
Using Jonathan suggestion on using usort, you can define your custom rules for sorting in a separate function, like:
function getAnimalValue($animal) {
switch($animal) {
case 'Lion':
return 1;
case 'Dog':
return 2;
case 'Zeebra':
return 3;
case 'Cat':
return 4;
}
return 0;
}
Then, implement your own compare function:
function compare($itemA, $itemB) {
$a = getAnimalValue($itemA['animal']);
$b = getAnimalValue($itemB['animal']);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
Finally, call usort using the compare function:
usort($array, "compare");
Check usort. Heres the reference:
http://www.php.net/manual/en/function.usort.php
Example:
function cmp($a, $b) {
$order=array("Lion","Dog","Zebra","Cat");
if ($a["animal"] == $b["animal"]) {
return 0;
}
return (array_search($a["animal"],$order) < array_search($b["animal"],$order)) ? -1 : 1;
}
$array = array(
array("id" => 7867867, "animal" => "Dog"),
array("id" => 3452342, "animal" => "Lion"),
array("id" => 1231233, "animal" => "Lion"),
array("id" => 5867867, "animal" => "Dog"),
array("id" => 1111111, "animal" => "Zebra"),
array("id" => 2222222, "animal" => "Cat"),
array("id" => 3333333, "animal" => "Cat"),
array("id" => 4444444, "animal" => "Zebra")
);
$mySortedArray=usort($array, "cmp");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With