Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Lisp

Idiom #100 Sort by a comparator

Sort elements of array-like collection items, using a comparator c.

(sort items #'c)

;;; c is a predicate like (defun c ( u v ) ( > u v ))
;;; items is a list
;;; the sorted list is
;;; note that #'c denotes the function c.
with Ada.Containers.Vectors;
use Ada.Containers;
type Integer_Comparator is not null access function (Left, Right : Integer) return Boolean;
      
package Integer_Vectors is new Vectors (Positive, Integer);
use Integer_Vectors;
      
procedure Sort_Using_Comparator (V : in out Vector; C : Integer_Comparator) is
   package Vector_Sorting is new Generic_Sorting (C.all);
   use Vector_Sorting;
         
begin
   Sort (V);
end Sort_Using_Comparator;

New implementation...
< >
programming-idioms.org