TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder on August 13, 2019, 08:24:49 AM

Title: Sort issue
Post by: Coder on August 13, 2019, 08:24:49 AM
Hello again guys,  :-)

I am looking for a way to sort a list based on the second item then the second item has to be sorted based on the minimum first number then the second then the third.

Code: [Select]
'(("CABLE" "50 70 40" "Yes" "NOTED" 100)
  ("CABLE" "40 50 25" "NO"  ""      000)
  ("CABLE" "50 10 50" "Yes" "NOTED" 200)
  ("CABLE" "10 80 60" "Yes" "NOTED" 300)
  )
Final result:

'(("CABLE" "10 80 60" "Yes" "NOTED" 300)
  ("CABLE" "40 50 25" "NO"  ""          000)
  ("CABLE" "50 10 50" "Yes" "NOTED" 200)
  ("CABLE" "50 70 40" "Yes" "NOTED" 100)
  )

Thank you
Title: Re: Sort issue
Post by: ribarm on August 13, 2019, 09:12:16 AM
Code: [Select]
(setq lst
 '(
    ("CABLE" "50 70 40" "Yes" "NOTED" 100)
    ("CABLE" "40 50 25" "NO"  ""      000)
    ("CABLE" "50 10 50" "Yes" "NOTED" 200)
    ("CABLE" "10 80 60" "Yes" "NOTED" 300)
  )
)

(defun str->lst ( str )
  (read (strcat "(" str ")"))
)

(setq lstn (vl-sort lst '(lambda ( a b ) (if (= (car (str->lst (cadr a))) (car (str->lst (cadr b)))) (if (= (cadr (str->lst (cadr a))) (cadr (str->lst (cadr b)))) (< (caddr (str->lst (cadr a))) (caddr (str->lst (cadr b)))) (< (cadr (str->lst (cadr a))) (cadr (str->lst (cadr b))))) (< (car (str->lst (cadr a))) (car (str->lst (cadr b))))))))

HTH.
Title: Re: Sort issue
Post by: ronjonp on August 13, 2019, 09:18:18 AM
This simple sort works for what your result list looks like:
Code - Auto/Visual Lisp: [Select]
  1. (vl-sort '(("CABLE" "50 70 40" "Yes" "NOTED" 100)
  2.            ("CABLE" "40 50 25" "NO" "" 000)
  3.            ("CABLE" "50 10 50" "Yes" "NOTED" 200)
  4.            ("CABLE" "10 80 60" "Yes" "NOTED" 300)
  5.           )
  6.          '(lambda (r j)(< (cadr r)(cadr j)))
  7. )
Title: Re: Sort issue
Post by: Coder on August 13, 2019, 09:28:36 AM
Thank you Marko, you codes work perfectly.

Thank you ronjonp, you codes are short and get the job done perfectly and beautifully also.  :grinwink:
Title: Re: Sort issue
Post by: ribarm on August 13, 2019, 09:30:05 AM
You are right Ron, but my metod offers more control over sorting by metodology OP requested... For ex. OP changed his mind and now wants to sort by second element of second string firstly, then by third element and then by first element... He just have to little think over and slightly change sorting function, but keep prinicip...
Title: Re: Sort issue
Post by: Lee Mac on August 13, 2019, 11:48:53 AM
Another, for any number of elements in the second position:
Code - Auto/Visual Lisp: [Select]
  1. (defun mysort ( l )
  2.     (mapcar '(lambda ( n ) (nth n l))
  3.         (vl-sort-i (mapcar '(lambda ( x ) (read (strcat "(" (cadr x) ")"))) l) 'compare)
  4.     )
  5. )
  6. (defun compare ( a b )
  7.     (cond
  8.         (   (null a) b)
  9.         (   (null b) nil)
  10.         (   (= (car a) (car b)) (compare (cdr a) (cdr b)))
  11.         (   (< (car a) (car b)))
  12.     )
  13. )

@Ron: you may get unexpected results for single digits ;)
Title: Re: Sort issue
Post by: Coder on August 13, 2019, 01:33:45 PM
Thanks Lee, that's another perfect function.  :smitten: