Sort all even numbers in ascending order and then sort all odd numbers in descending order | To do two way sort . First sort even numbers in ascending order , then odd numbers in descending order . ; Make all odd numbers negative ; if ( $arr [ $i ] & 1 ) Check for odd ; Sort all numbers ; Retaining original array ; Driver code
< ? php function twoWaySort ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] *= -1 ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] & 1 ) $ arr [ $ i ] *= -1 ; } $ arr = array ( 1 , 3 , 2 , 7 , 5 , 4 ) ; $ n = sizeof ( $ arr ) ; twoWaySort ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >