Programming-Idioms

History of Idiom 28 > diff from v26 to v27

Edit summary for version 27 :
New Ada implementation by user [Smaehtin]

Version 26

Version 27

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of type Item of the objects in items.

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of type Item of the objects in items.

Imports
with Ada.Containers.Vectors;
use Ada.Containers;
Code
declare
   function Compare_Function (Left, Right : Item) return Boolean is (Left.P < Right.P);
   
   package Item_Vector_Sorting is new Item_Vectors.Generic_Sorting (Compare_Function);

   use Item_Vector_Sorting;
begin
   Sort (Items);
end;