Sort all even numbers in ascending order and then sort all odd numbers in descending order | Java program sort array in even and odd manner . The odd numbers are to be sorted in descending order and the even numbers in ascending 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 ) != 0 ) Check for odd ; Sort all numbers ; Retaining original array ; Driver Method
import java . util . Arrays ; public class GFG { static void twoWaySort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] *= - 1 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 7 , 5 , 4 } ; twoWaySort ( arr , arr . length ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }