-- Demonstrates creating and calling a generic array sort
with ada.text_io; use ada.text_io;
with ada.float_text_io; use ada.float_text_io;
with Ada.Containers.Generic_Array_Sort;
procedure generic_sort2 is
type Person is record
ID: Natural;
age: Natural;
end record;
type Person_Array is array(Natural range <>) of Person;
procedure put_line(a: Person_Array) is
begin
for p of a loop
put(p.ID'img & p.age'img & ", ");
end loop;
new_line;
end put_line;
function id_less(left, right: Person) return Boolean is
(left.id < right.id);
procedure sort is new
Ada.Containers.Generic_Array_Sort(Natural, Person, Person_Array, id_less);
-- Uses default version of < for strings
function younger(left, right: Person) return Boolean is
(left.age < right.age);
procedure young_sort is new Ada.Containers.Generic_Array_Sort(
Index_Type => Natural,
Element_Type => Person,
Array_Type => Person_Array,
"<" => younger);
people : Person_Array(1 .. 4) := ((30, 10), (40, 5), (20, 15), (25, 99));
begin
put_line(people);
sort(people);
put_line(people);
young_sort(people);
put_line(people);
end generic_sort2;
-- Output