vl-sort-i

AutoCad AutoLISP Functions

 
vl-sort-i
 
 
 

Sorts the elements in a list according to a given compare function, and returns the element index numbers

(vl-sort-i  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 index values of the elements of list, sorted in the order specified by comparison-function. Duplicate elements will be retained in the result.

Examples

Sort a list of characters in descending order:

_$ (vl-sort-i '("a" "d" "f"
"c") '>)
(2 1 3 0)

The sorted list order is “f” “d” “c” “a”; “f” is the 3rd element (index 2) in the original list, “d” is the 2nd element (index 1) in the list, and so on.

Sort a list of numbers in ascending order:

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

Note that both occurrences of 3 are accounted for in the result list.

Sort a list of 2D points by Y coordinate:

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

Sort a list of symbols:

_$ (vl-sort-i 
         '(a d c b a)
    
         '(lambda (s1 s2)
    
          (< (vl-symbol-name
s1) (vl-symbol-name s2)) ) )
    
(4 0 3 2 1)

Note that both a's are accounted for in the result list.