Sorting objects using In | C # implementation of the approach ; 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 ; Driver code ; Let 's represent objects as strings
using System ; using System . Collections . Generic ; class GFG { static int partition ( List < String > objects , int l , int r , Dictionary < String , int > hash ) { int j = l - 1 ; String temp ; int last_element = hash [ objects [ r ] ] ; for ( int i = l ; i < r ; i ++ ) { if ( hash [ objects [ i ] ] <= last_element ) { j ++ ; temp = objects [ i ] ; objects [ i ] = objects [ j ] ; objects [ j ] = temp ; } } j ++ ; temp = objects [ r ] ; objects [ r ] = objects [ j ] ; objects [ j ] = temp ; return j ; } static void quicksort ( List < String > objects , int l , int r , Dictionary < String , int > hash ) { if ( l < r ) { int mid = partition ( objects , l , r , hash ) ; quicksort ( objects , l , mid - 1 , hash ) ; quicksort ( objects , mid + 1 , r , hash ) ; } } static void sortObj ( List < String > objects ) { Dictionary < String , int > hash = new Dictionary < String , int > ( ) ; hash . Add ( " blue " , 1 ) ; hash . Add ( " red " , 2 ) ; hash . Add ( " yellow " , 3 ) ; quicksort ( objects , 0 , objects . Count - 1 , hash ) ; for ( int i = 0 ; i < objects . Count ; i ++ ) Console . Write ( objects [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { List < String > objects = new List < String > { " red " , " blue " , " red " , " yellow " , " blue " } ; sortObj ( objects ) ; } }