usort sorting multiple fields boolean and number
i hope this answer will satisfied you
$cmp = function ($a, $b) {
if ($a['active'] == $b['active']) {
return $a['age'] < $b['age'] ? -1 : 1;
} else if ($a['active'] == true) {
return -1;
} else {
return 1;
}
};
usort($arr, $cmp);
How to sort php array by multiple fields using usort
usort($childs, function($a, $b) {
if ($a['field1'] == $b['field1']) {
if ($a['field2'] == $b['field2']) {
return $a['field3'] < $b['field3'] ? -1 : 1;
} else {
return 0;
}
return $a['field2'] < $b['field2'] ? -1 : 1;
}
return $a['field1'] < $b['field1'] ? -1 : 1;
});
EDIT
a slightly more generic solution (untested)
$sorts = array('field1' => 'asc', 'field2' => 'asc', 'field3' => 'asc');
usort($childs, function($a, $b) use (array $sorts = array()) {
foreach($sorts as $field => $direction) {
if ($a[$field] != $b[$field]) {
if ($direction == 'asc') {
return $a[$field] < $b[$field] ? -1 : 1;
}
return $a[$field] < $b[$field] ? 1 : -1;
}
}
return 0;
});
usort array by two parameters using spaceship operator
usort($products, function ($a, $b) {
if ( $a['brand_order'] == $b["brand_order"] ) { //brand_order are same
return $a['title'] <=> $b['title']; //sort by title
}
return $a['brand_order'] <=> $b['brand_order']; //else sort by brand_order
});
Test here
PHP sort array by two field values
array_multisort()
is the correct function, you must have messed up somehow:
// Obtain a list of columns
foreach ($data as $key => $row) {
$return_fare[$key] = $row['return_fare'];
$one_way_fare[$key] = $row['one_way_fare'];
}
// Sort the data with volume descending, edition ascending
array_multisort($data, $return_fare, SORT_ASC, $one_way_fare, SORT_ASC);
If you take a look at the comments at PHP's manual page for array_multisort()
, you can find a very helpful array_orderby()
function which allows you to shorten the above to just this:
$sorted = array_orderby($data, 'return_fare', SORT_ASC, 'one_way_fare', SORT_ASC);
To avoid the looping use array_column()
(as of PHP 5.5.0):
array_multisort(array_column($data, 'return_fare'), SORT_ASC,
array_column($data, 'one_way_fare'), SORT_ASC,
$data);
usort: how I can sort by multiple data types?
Based on: PHP.net:multisort()
As you have stated in your question, this is just the basics of how it can be done. To make the whole thing more dynamic, you would need a way to pass the sorting choices into a function that then uses them in the foreach to control which sorting arrays are used...
<?php
//Build example data
$data = array();
$data[] = array('priority'=>2,'title'=>'B');
$data[] = array('priority'=>3,'title'=>'C');
$data[] = array('priority'=>2,'title'=>'A');
$data[] = array('priority'=>1,'title'=>'A');
$data[] = array('priority'=>3,'title'=>'D');
foreach ($data as $key => $row) {
$priority[$key] = $row['priority'];
$title[$key] = $row['title'];
}
array_multisort($priority, SORT_ASC, $title, SORT_ASC, $data);
print_r($data);
?>
How to sort an array of objects by multiple fields?
You could use a chained sorting approach by taking the delta of values until it reaches a value not equal to zero.
var data = [{ h_id: "3", city: "Dallas", state: "TX", zip: "75201", price: "162500" }, { h_id: "4", city: "Bevery Hills", state: "CA", zip: "90210", price: "319250" }, { h_id: "6", city: "Dallas", state: "TX", zip: "75000", price: "556699" }, { h_id: "5", city: "New York", state: "NY", zip: "00010", price: "962500" }];
data.sort(function (a, b) { return a.city.localeCompare(b.city) || b.price - a.price;});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Sorting 2 fields ASC in array with usort (PHP)
That's because you are not comparing the usernames -- you are subtracting them numerically, which gives totally inappropriate results (see string conversion to numbers).
Replace return $a['user'] - $b['user']
with return strcmp($a['user'], $b['user'])
to get the expected result.
Shameless self-promotion: You could also consider using the handy sorting technique I give here to get the correct result with much more appealing code:
usort($new, make_comparer('date', 'user'));
Related Topics
PHP PDF Template Library with PDF Output
How to Block Disposable Email Addresses in Your Website's Registration Form
Laravel Middleware Return Variable to Controller
How to Overload Class Constructor Within Traits in PHP >= 5.4
Get Root Directory Path of a PHP Project
When to Use a Class VS. Function in PHP
PHP Date Time Greater Than Today
How to Enable Enums in Symfony 2/Doctrine
How to Implement a Chat Room Using Jquery/Php
Is MySQLi_Multi_Query Asynchronous
Laravel 5.3 New Auth::Routes()