Author Topic: Sort issue  (Read 2543 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Sort issue
« 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

ribarm

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

:)

M.R. on Youtube

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Sort issue
« Reply #2 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. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Coder

  • Swamp Rat
  • Posts: 827
Re: Sort issue
« Reply #3 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:

ribarm

  • Gator
  • Posts: 3265
  • Marko Ribar, architect
Re: Sort issue
« Reply #4 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...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Sort issue
« Reply #5 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 ;)

Coder

  • Swamp Rat
  • Posts: 827
Re: Sort issue
« Reply #6 on: August 13, 2019, 01:33:45 PM »
Thanks Lee, that's another perfect function.  :smitten: