Sorting an array keeping even values ​​at the top of PHP

I tried sorting the array below to keep the even values ​​from the top and in sorted order from asc

todesc

$array = array(1,2,3,4,5,6,7,8,9,10);

      

I tried this for sorting the array

usort($array, function($a, $b) { 
                    if ($a % 2 == 0 )
                    { 
                        return 1 ;
                    }
                    else 
                    { 
                        return -1;
                    }
            }
        );

      

I got the output as below

Array
(
    [0] => 7
    [1] => 9
    [2] => 1
    [3] => 5
    [4] => 3
    [5] => 2
    [6] => 4
    [7] => 6
    [8] => 8
    [9] => 10
)

      

And I want the output array to be

Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
    [5] => 1
    [6] => 3
    [7] => 5
    [8] => 7
    [9] => 9
)

      

Even and odd values ​​must be sorted in ascending descending order, but keep the even values ​​on top of the odd values

+3


source to share


5 answers


usort

not stable . The documentation states:

If two members compare as equal, their relative order in the sorted array is undefined.

So what you can do is:



usort($array, function($a, $b) {
                if ($a % 2 == $b % 2) {
                    return intval($a - $b);
                }
                elseif ($a % 2 == 0 )
                { 
                    return -1 ;
                }
                else 
                { 
                    return 1;
                }
        }
    );

      

This compares the actual values ​​if both are even or both are odd.

+6


source


I think you need a slightly more complex function. Because when you look at this, there are different cases to take care of, and in each of them something different has to happen:

  • $a

    and $b

    both are even: default numeric comparison
  • $a

    and $b

    both are odd: default numeric comparison
  • $a

    even, $b

    odd: $a

    always less$b

  • $a

    odd, $b

    equal: $b

    always less$a



And see fab's answer for the implementation .

+4


source


A bit lenghty, but works great for me:

$array = array(1,2,3,4,5,6,7,8,9,10);

$even =array();

$odd = array();

foreach ($array as $item) {

  if($item%2==0) {

    $even[] = $item;
  }

  else {

    $odd[] = $item;

  }

}

usort($even);

usort($odd);

$array_sort = array_merge($even,$odd);

print_r($array_sort);

      

+2


source


usort($array, function($a, $b) { 
                    if ($a % 2 == $b % 2) {
                           if ($a == $b) {
                               return 0;
                           }
                           return ($a < $b) ? -1 : 1;
                    } elseif ($a % 2 == 0) { 
                        return -1 ;
                    } else { 
                        return 1;
                    }
            }
        );

      

+2


source


Short and easy way to write:

<?php
$array = array(1,2,3,4,5,6,7,8,9,10);
usort($array, function($a, $b){
  if ($a % 2 == $b % 2) {
    return $a - $b;
  }
  return $a % 2 != 0;
});
print_r($array);
?>

      

or with triple:

<?php
$array = array(1,2,3,4,5,6,7,8,9,10);
usort($array, function($a, $b){
  return ($a % 2 == $b % 2) ? ($a - $b) : ($a % 2 != 0);
});
print_r($array);
?>

      

+1


source







All Articles