Sorting objects using In | Partition function which will partition the array and into two parts ; Compare hash values of objects ; Classic quicksort algorithm ; Function to sort and print the objects ; Create a hash table ; As the sorting order is blue objects , red objects and then yellow objects ; Quick sort function ; Printing the sorted array ; Let 's represent objects as strings
function partition ( objects , l , r , hash ) { let j = l - 1 ; let last_element = hash . get ( objects [ r ] ) ; for ( let i = l ; i < r ; i ++ ) { if ( hash . get ( objects [ i ] ) <= last_element ) { j ++ ; let temp = objects [ i ] ; objects [ i ] = objects [ j ] ; objects [ j ] = temp ; } } j ++ ; let temp = objects [ r ] ; objects [ r ] = objects [ j ] ; objects [ j ] = temp ; return j ; } function quicksort ( objects , l , r , hash ) { if ( l < r ) { let mid = partition ( objects , l , r , hash ) ; quicksort ( objects , l , mid - 1 , hash ) ; quicksort ( objects , mid + 1 , r , hash ) ; } } function sortObj ( objects ) { let hash = new Map ( ) ; hash . set ( " " , 1 ) ; hash . set ( " " , 2 ) ; hash . set ( " " , 3 ) ; quicksort ( objects , 0 , objects . length - 1 , hash ) ; for ( let i = 0 ; i < objects . length ; i ++ ) document . write ( objects [ i ] + " " ) ; } let objects = [ " " , " " , " " , " " , " " ] ; sortObj ( objects ) ;