I have to sort 2d array
$items = array(
array(15, 16, 8, 1),
array(2, 3, 4, 7),
array(9, 11, 19, 6,)
);
with bubble sort ant to get something like this
1,2,3,4
6,7,8,9
11,15,16,19
I can't find anywhere bubble sort for multiple arrays. Can you help me?
I tried something like this, but it isn't working:
$iterations = 0;
for ($i = 0; $i < count($array); $i++)
{
$iterations++;
$hasSwap = false;
for ($j = 0; $j < count($array) - 1 - $i; $j++)
{
$iterations++;
if ($array[$j] > $array[$j + 1])
{
$hasSwap = true;
swap($array, $j, $j + 1);
}
}
if (!$hasSwap)
{
break;
}
}
var_dump($iterations);
print_r($array);
You are treating the three arrays as one. So
Probably someone will be useful this decision. Here it was necessary to sort the elements by bubbles based on the value of 'COUNT'
within each element.
$fruits
(
[0] => Array
(
[NAME] => Banana
[COUNT] => 7
)
[1] => Array
(
[NAME] => Orange
[COUNT] => 2
)
[2] => Array
(
[NAME] => Apple
[COUNT] => 24
)
)
usort($fruits, function($a, $b)
{
if($a['COUNT'] > $b['COUNT'])
{
return -1;
}
if($a['COUNT'] < $b['COUNT'])
{
return 1;
}
return 0;
});
More examples and detailed of using: usort
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.