vl-sort

AutoCad AutoLISP Functions

 
vl-sort
 
 
 

Sorts the elements in a list according to a given compare function

(vl-sort  list comparison-function)

Arguments

list

Any list.

comparison-function

A comparison function. This can be any function that accepts two arguments and returns T (or any non-nil value) if the first argument precedes the second in the sort order. The comparison-function value can take one of the following forms:

  • A symbol (function name)
  • '(LAMBDA (A1 A2) ...)
  • (FUNCTION (LAMBDA (A1 A2) ...))

Return Values

A list containing the elements of list in the order specified by comparison-function. Duplicate elements may be eliminated from the list.

Examples

Sort a list of numbers:

_$ (vl-sort '(3 2 1 3) '<)
(1 2 3)     ;  

Note that the result list contains only one 3.

Sort a list of 2D points by Y coordinate:

_$ (vl-sort '((1 3) (2 2)
(3 1))
                   (function
(lambda (e1 e2)
    
                           
   (< (cadr e1) (cadr e2)) ) ) )
    
((3 1) (2 2) (1 3))

Sort a list of symbols:

_$ (vl-sort 
         '(a d c b a)
    
         '(lambda (s1 s2)
    
          (< (vl-symbol-name
s1) (vl-symbol-name s2)) ) )
    
(A B C D)       ;  Note that only one A remains in the result list