Sort first k values in ascending order and remaining n | function to sort the array ; Sort first k elements in ascending order ; Sort remaining n - k elements in descending order ; Our arr contains 8 elements
function printOrder ( arr , k ) { let n = arr . length ; arr = arr . slice ( 0 , k ) . sort ( function ( a , b ) { return a - b ; } ) . concat ( arr . slice ( k , n ) . sort ( function ( a , b ) { return b - a ; } ) ) ; return arr ; } let arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 ] ; let k = 4 ; arr = printOrder ( arr , k ) ; document . write ( arr . join ( " " ) ) ;