generic -- GENERIC_SORT ( ELEMENT , INDEX , VECTOR , ">" ) type ELEMENT is private ; -- simple objects, records or arrays type INDEX is ( <> ) ; -- any discrete type type VECTOR is array ( INDEX range <> ) of ELEMENT ; -- structure form -- a comparison function must be provided if ELEMENT -- is not a type or subtype for which ">" is visible. -- ONLY the one comparison function is needed. -- return true if the first parameter is larger than the second with function ">" ( SMALL , LARGE : ELEMENT ) return BOOLEAN is <> ; package GENERIC_SORT is procedure SORT ( ARR1 : in out VECTOR ) ; end GENERIC_SORT ; -- This is a demonstration of GENERIC_SORT with arrays of records subtype MY_STRING is STRING ( 1 .. 3 ) ; type MY_ENUM is ( FIRST , SECOND , THIRD , FOURTH ) ; type MY_RECORD is -- a vector of this type of record will be sorted record I : INTEGER ; ENUM_PART : MY_ENUM ; X : FLOAT ; STRING_PART : MY_STRING ; end record ; -- Example , sorting an array of records indexed by an enumeration type type MY_ENUM_ARRAY is array ( MY_ENUM range <> ) of MY_RECORD ; ENUM_RECORD_DATA : MY_ENUM_ARRAY ( MY_ENUM ) := ( ( 1 , THIRD , 3.0 , "BBB" ) , ( 2 , FOURTH , 1.0 , "AAA" ) , ( 3 , SECOND , 2.0 , "BBB" ) , ( 4 , THIRD , 4.0 , "CCC" ) ) ; -- When sorting records there is no predefined comparison operator -- Generally some part of the record is the key by which the -- records are sorted. This example shows a primary key that is -- ENUM_PART and the use of a secondary key STRING_PART when -- the primary keys of the two records are equal. -- Any code may be used in the function. The purpose of the -- function is to return TRUE if LEFT > RIGHT . -- Example comparison function for ">" function MY_ENUM_COMPARE ( LEFT , RIGHT : MY_RECORD ) return BOOLEAN is begin return ( LEFT.ENUM_PART > RIGHT.ENUM_PART ) or ( LEFT.ENUM_PART = RIGHT.ENUM_PART and LEFT.STRING_PART > RIGHT.STRING_PART ) ; end MY_ENUM_COMPARE ; -- Example instantiation of GENERIC_SORT package ENUM_RECORD_SORT is new GENERIC_SORT ( MY_RECORD , MY_ENUM , MY_ENUM_ARRAY , MY_ENUM_COMPARE ) ; -- Example , perform the sort and print the results ENUM_RECORD_SORT.SORT ( ENUM_RECORD_DATA ) ;