PHP Sort Array by Two Field Values

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);

Sort multidimensional array by multiple columns

You need array_multisort

$mylist = array(
array('ID' => 1, 'title' => 'Boring Meeting', 'event_type' => 'meeting'),
array('ID' => 2, 'title' => 'Find My Stapler', 'event_type' => 'meeting'),
array('ID' => 3, 'title' => 'Mario Party', 'event_type' => 'party'),
array('ID' => 4, 'title' => 'Duct Tape Party', 'event_type' => 'party')
);

# get a list of sort columns and their data to pass to array_multisort
$sort = array();
foreach($mylist as $k=>$v) {
$sort['title'][$k] = $v['title'];
$sort['event_type'][$k] = $v['event_type'];
}
# sort by event_type desc and then title asc
array_multisort($sort['event_type'], SORT_DESC, $sort['title'], SORT_ASC,$mylist);

As of PHP 5.5.0:

array_multisort(array_column($mylist, 'event_type'), SORT_DESC,
array_column($mylist, 'title'), SORT_ASC,
$mylist);

$mylist is now:

array (
0 =>
array (
'ID' => 4,
'title' => 'Duct Tape Party',
'event_type' => 'party',
),
1 =>
array (
'ID' => 3,
'title' => 'Mario Party',
'event_type' => 'party',
),
2 =>
array (
'ID' => 1,
'title' => 'Boring Meeting',
'event_type' => 'meeting',
),
3 =>
array (
'ID' => 2,
'title' => 'Find My Stapler',
'event_type' => 'meeting',
),
)

PHP - sort array by 2 values

You can do it using array_multisort(). Your code should be :

    $arr = array(
array(
"number1"=>100,
"number2"=>24
),
array(
"number1"=>100,
"number2"=>22
),
array(
"number1"=>300,
"number2"=>30
),
array(
"number1"=>200,
"number2"=>23
));
// Make a $tempArr for list of sort columns and their data to pass to array_multisort function
$tempArr = array();

foreach($arr as $key=>$val) {
$tempArr['number1'][$key] = $val['number1'];
$tempArr['number2'][$key] = $val['number2'];
}
// sort by number1 asc and then number2 asc
array_multisort($tempArr['number1'], SORT_ASC, $tempArr['number2'], SORT_ASC,$arr);

print_r($arr);

PHP sort array of objects by two properties

Why the extra level of indirection and making things more confusing? Why not usort directly with usort($objectArray, "sortObjects"); using a sortObjects($a,$b) function that does what any comparator does: return negative/0/positive numbers based on the input?

If the tabs differ, return their comparison, if they're the same, return the order comparison; done.

$array = array(
(object)array(
'tab_option_name_selector' => 2,
'fieldtype' => 'notes',
'order' => 12
),
(object)array(
'tab_option_name_selector' => 2,
'fieldtype' => 'notes',
'order' => 8
),
(object)array(
'tab_option_name_selector' => 1,
'order' => 2,
'fieldtype' => 'selectbox'
),
(object)array(
'tab_option_name_selector' => 2,
'order' => 3,
'fieldtype' => 'selectbox'
)
);

function compareTabAndOrder($a, $b) {
// compare the tab option value
$diff = $a->tab_option_name_selector - $b->tab_option_name_selector;
// and return it. Unless it's zero, then compare order, instead.
return ($diff !== 0) ? $diff : $a->order - $b->order;
}

usort($array, "compareTabAndOrder");
print_r($array);

Sort an array of associative arrays by multiple columns using a combination of ascending, descending, regular, numeric, and natural sorting

I think you have to implement a custom comparison function for this behavior:

function myCmp($a, $b) {
$nameCmp = strnatcasecmp($a['Name'], $b['Name']);
$ageCmp = strnatcasecmp($a['Age'], $b['Age']);
$codeCmp = strnatcasecmp($a['Code'], $b['Code']);

if ($nameCmp != 0) // Names are not equal
return($nameCmp);

// Names are equal, let's compare age

if ($ageCmp != 0) // Age is not equal
return($ageCmp * -1); // Invert it since you want DESC

// Ages are equal, we don't need to compare code, just return the comparison result
return($codeCmp);
}

Then you can call usort($array, 'myCmp'); and should get the desired sorting

sort array of objects by two fields in php

If you want to sort after an other criteria, you should just add the condition to your sort function:

if ($a->pin != $b->pin) return $a->pin == 1 ? -1 : 1;

this condition only applies for $items with unequal pin properties. Combine your old compare function with this one would result in the following

function cmp($a, $b) {
if ($a->pin != $b->pin) return $a->pin == 1 ? -1 : 1;
return strcmp($a->date, $b->date);
}

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



Related Topics



Leave a reply



Submit