Author Topic: Sort texts list  (Read 264 times)

0 Members and 1 Guest are viewing this topic.

civil.eng

  • Newt
  • Posts: 47
Sort texts list
« on: July 05, 2021, 09:07:12 AM »
Hi all,
I need a function to sort a list by first element , like this :

(("3" Xc Yc) ("1" Xc Yc) ("2" Xc Yc))              >> (("1" Xc Yc) ("2" Xc Yc) ("3" Xc Yc))

(("003" Xc Yc) ("001" Xc Yc) ("002" Xc Yc))   >> (("001" Xc Yc) ("002" Xc Yc) ("003" Xc Yc))

(("A3" Xc Yc) ("A1" Xc Yc) ("A2" Xc Yc))        >> (("A1" Xc Yc) ("A2" Xc Yc) ("A3" Xc Yc))

(("B" Xc Yc) ("C" Xc Yc) ("A" Xc Yc))             >>  (("A" Xc Yc) ("B" Xc Yc) ("C" Xc Yc))

I mean the function should work on the above lists, Xc and Yc are center point of a polyline.

I'm using this code to sort :
Code: [Select]
(vl-sort
lst
'(lambda (x y)
    (< (atoi (car x))
       (atoi (car y))
    )
  )
 )


But the code only works on the first and the second lists.
« Last Edit: July 05, 2021, 09:10:14 AM by civil.eng »

ribarm

  • Gator
  • Posts: 2527
  • Marko Ribar, architect
Re: Sort texts list
« Reply #1 on: July 05, 2021, 09:11:53 AM »
Have you tried, just :

Code: [Select]
(vl-sort lst '(lambda ( a b ) (< (car a) (car b))))
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

civil.eng

  • Newt
  • Posts: 47
Re: Sort texts list
« Reply #2 on: July 05, 2021, 10:37:53 AM »
Yes I have, but the numbers sorted like this :

"1" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "2" "20" "3"

"p1" "p10" "p11" "p12" "p13" "p14" "p2" "p3" "p4" "p4"  "p5" "p6"  "p7"  "p8" "p9"

ribarm

  • Gator
  • Posts: 2527
  • Marko Ribar, architect
Re: Sort texts list
« Reply #3 on: July 05, 2021, 11:55:13 AM »
I'd rather change initial list, than change sorting algorithm - still you can search for Lee Mac's (LM:alphanumsort) function and use it...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

myloveflyer

  • Newt
  • Posts: 148
Re: Sort texts list
« Reply #4 on: July 05, 2021, 11:41:48 PM »
Code: [Select]
;; Alphanumerical Sort  -  Lee Mac
  ;; Sorts a list of strings containing a combination of alphabetical & numerical characters.
  (defun LM:alphanumsort (lst)
    (mapcar (function (lambda (n) (nth n lst)))
    (vl-sort-i (mapcar 'LM:splitstring lst)
       (function (lambda (a b / x y)
   (while (and (setq x (car a)) (setq y (car b)) (= x y))
     (setq a (cdr a)
   b (cdr b)
     )
   )
   (cond ((null x) b)
((null y) nil)
((and (numberp x) (numberp y)) (< x y))
((numberp x))
((numberp y) nil)
((< x y))
   )
)
       )
    )
    )
  )
  ;; Split String  -  Lee Mac
  ;; Splits a string into a list of text and numbers
  (defun LM:splitstring (str)
    ((lambda (l)
       (read
(strcat "("
(vl-list->string
   (apply 'append
  (mapcar (function
    (lambda (a b c)
      (cond ((= 92 b) (list 32 34 92 b 34 32))
    ((or (< 47 b 58) (and (= 45 b) (< 47 c 58) (not (< 47 a 58))) (and (= 46 b) (< 47 a 58) (< 47 c 58)))
     (list b)
    )
    ((list 32 34 b 34 32))
      )
    )
  )
  (cons nil l)
  l
  (append (cdr l) '(()))
  )
   )
)
")"
)
       )
     )
      (vl-string->list str)
    )
  )
Never give up !