Author Topic: sort a list  (Read 1410 times)

0 Members and 1 Guest are viewing this topic.

motee-z

  • Newt
  • Posts: 40
sort a list
« 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

ribarm

  • Gator
  • Posts: 3249
  • Marko Ribar, architect
Re: sort a list
« Reply #1 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.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ronjonp

  • Needs a day job
  • Posts: 7527
Re: sort a list
« Reply #2 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)) '>))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: sort a list
« Reply #3 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)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: sort a list
« Reply #4 on: October 07, 2022, 12:52:59 PM »
Hi,
You can have a look at this (old) topic.
Speaking English as a French Frog

motee-z

  • Newt
  • Posts: 40
Re: sort a list
« Reply #5 on: October 08, 2022, 04:52:11 AM »
thanks for all