Here are two ways, the second is far more efficient than the first because there are far fewer operations within the sort predicate function.
(defun SortByZ ( lst )
(vl-sort lst
(function
(lambda ( a b )
(< (apply 'max (mapcar 'caddr a)) (apply 'max (mapcar 'caddr b)))
)
)
)
)
(defun SortByZ2 ( lst )
(mapcar
(function
(lambda ( n ) (nth n lst))
)
(vl-sort-i (mapcar '(lambda ( x ) (apply 'max (mapcar 'caddr x))) lst) '<)
)
)
_$ (Benchmark '((SortbyZ lst) (SortbyZ2 lst)))
Benchmarking ................Elapsed milliseconds / relative speed for 8192 iteration(s):
(SORTBYZ2 LST).....1295 / 2.69 <fastest>
(SORTBYZ LST)......3479 / 1 <slowest>
For a list with 100 triangles:
(defun test ( / lst sub1 sub2 )
(repeat 100
(repeat 3
(repeat 3
(setq sub1 (cons (_rand) sub1))
)
(setq sub2 (cons sub1 sub2) sub1 nil)
)
(setq lst (cons sub2 lst) sub2 nil)
)
(Benchmark '((SortbyZ lst) (SortbyZ2 lst)))
)
(defun _rand ( / mod )
(/ (setq mod 4294967296.0 seed (rem (1+ (* 1664525.0 (cond (seed) ((getvar 'DATE))))) mod)) mod)
)
_$ (test)
Benchmarking .............Elapsed milliseconds / relative speed for 1024 iteration(s):
(SORTBYZ2 LST).....1357 / 7.33 <fastest>
(SORTBYZ LST)......9953 / 1 <slowest>
Note that since your example list contains duplicate maximum Z-values, the above functions may return different results.
For more information about how to use
mapcar/lambda, perhaps have a read of
this short tutorial.
More information about the Sorting process can be found
here.