TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: motee-z on October 07, 2022, 07:20:25 AM

Title: sort a list
Post by: motee-z on October 07, 2022, 07:20:25 AM
Hello
whow to sort a list containing  equal numbers and keeping equal numbers
example
(setq lst(list 5 6 4 8 9 6 4 )
must return(9 8 6 6 5 4 4)
thanks
Title: Re: sort a list
Post by: ribarm on October 07, 2022, 07:54:24 AM
Code - Auto/Visual Lisp: [Select]
  1. (defun _vl-sort ( l f / *q* ll ff gg )
  2.   (if (= (type f) (quote sym))
  3.     (setq f (eval f))
  4.   )
  5.   (while (setq *q* (car l))
  6.     (setq ll
  7.       (if (null ll)
  8.         (cons *q* ll)
  9.         (cond
  10.           ( (apply f (list (last ll) *q*))
  11.             (append ll (list *q*))
  12.           )
  13.           ( (apply f (list *q* (car ll)))
  14.             (cons *q* ll)
  15.           )
  16.           ( t
  17.             (setq ff nil)
  18.             (setq gg (apply (function append) (append (mapcar (function (lambda ( *xxx* *yyy* ) (if (null ff) (if (apply f (list *q* *yyy*)) (progn (setq ff t) (list *xxx* *q*)) (list *xxx*)) (list *xxx*)))) ll (cdr ll)) (list (list (last ll))))))
  19.             (if (null ff)
  20.               (append ll (list *q*))
  21.               gg
  22.             )
  23.           )
  24.         )
  25.       )
  26.     )
  27.     (setq l (cdr l))
  28.   )
  29.   ll
  30. )
  31.  
  32. (_vl-sort (list 5 6 4 8 9 6 4 ) (function >))
  33.  

Code - Auto/Visual Lisp: [Select]
  1. (mapcar (function (lambda ( x ) (nth x (list 5 6 4 8 9 6 4 )))) (vl-sort-i (list 5 6 4 8 9 6 4 ) (function >)))
  2.  
Title: Re: sort a list
Post by: ronjonp on October 07, 2022, 10:07:27 AM
Code - Auto/Visual Lisp: [Select]
  1. (mapcar 'fix (vl-sort (mapcar 'float '(5 6 4 8 9 6 4)) '>))
Title: Re: sort a list
Post by: Marc'Antonio Alessi on October 07, 2022, 12:02:15 PM
Code: [Select]
; Tony Tanzillo
(defun vl-sort_TT (lst func)
   (mapcar
     '(lambda (x) (nth x lst))
      (vl-sort-i lst func)
   )
)
(vl-sort_TT '(5 6 4 8 9 6 4) '>) => (9 8 6 6 5 4 4)
Title: Re: sort a list
Post by: gile on October 07, 2022, 12:52:59 PM
Hi,
You can have a look at this (old) topic (http://www.theswamp.org/index.php?topic=38292.msg433708#msg433708).
Title: Re: sort a list
Post by: motee-z on October 08, 2022, 04:52:11 AM
thanks for all