Sort all even numbers in ascending order and then sort all odd numbers in descending order | Utility function to print the contents of the array ; To do two way sort . Make comparator function for the inbuilt sort function of c ++ such that odd numbers are placed before even in descending and ascending order respectively ; If both numbers are even , smaller number should be placed at lower index ; If both numbers are odd larger number should be placed at lower index ; If a is odd and b is even , a should be placed before b ; If b is odd and a is even , b should be placed before a ; Driver code ; Sort the array ; Print the sorted array
function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function compare ( a , b ) { if ( a % 2 == 0 && b % 2 == 0 && a < b ) return - 1 ; if ( a % 2 != 0 && b % 2 != 0 && b > a ) return 1 ; if ( a % 2 != 0 ) return - 1 ; return 1 ; } var arr = [ 1 , 3 , 2 , 7 , 5 , 4 ] ; var n = arr . length ; arr . sort ( compare ) ; printArr ( arr , n ) ;