Sort list issue

Sort list issue

msarqui
Collaborator Collaborator
794 Views
4 Replies
Message 1 of 5

Sort list issue

msarqui
Collaborator
Collaborator

Hi everyone,

 

How do I sort this list :

'(("CC = E03-A") ("CC = E04-A") ("B = G03-A") ("A = G03-A") ("C = G03-A") ("A = T03-C") ("B = T03-C") ("C = T03-D") ("AA = T06-D") ("BB = T06-H") ("AA = T06-H") ("BB = T06-I"))

 

To have this result :

'(("A = G03-A") ("A = T03-C")  ("B = G03-A") ("B = T03-C") ("C = G03-A") ("C = T03-D") ("AA = T06-D") ("AA = T06-H") ("BB = T06-H")  ("BB = T06-I") ("CC = E03-A") ("CC = E04-A"))

 

Thanks in advance,

Marcelo

0 Likes
Accepted solutions (2)
795 Views
4 Replies
Replies (4)
Message 2 of 5

Ranjit_Singh
Advisor
Advisor
Accepted solution

There might be different ways of doing this. Here is one

(mapcar 'list (acad_strlsort (apply 'append lst)))

where lst is your list

 EDIT: That is a trick question Smiley Wink I just realized your output is not sorted alphabetically. Try this

(mapcar 'car (vl-sort (mapcar '(lambda (x) (cons x (strlen (vl-princ-to-string (read (car x)))))) (mapcar 'list (acad_strlsort (apply 'append lst)))) '(lambda (x y) (< (cdr x) (cdr y)))))

Again, there might be better ways of doing this that other members may suggest.

 

0 Likes
Message 3 of 5

msarqui
Collaborator
Collaborator
Thanks Ranjit.Singh
Your edit code was the solution.
0 Likes
Message 4 of 5

Kent1Cooper
Consultant
Consultant
Accepted solution

It seems to me you could at least eliminate the 

(vl-princ-to-string (read 

functions, and simply read the length of the strings directly:

 

(mapcar 'car
  (vl-sort
    (mapcar '(lambda (x) (cons x (strlen (car x))))
      (mapcar 'list (acad_strlsort (apply 'append lst)))
    )
    '(lambda (x y) (< (cdr x) (cdr y)))
  )
)

But that raises a question in my mind.  That seems to compare them by overall string length, assuming the only difference between any of them is whether they start with a single or a double letter.  Might there ever be some that are different in any other way, affecting their string length, such as perhaps [random variants on the first item here]:

 

("CC = E103-A")

 

or

 

("CC = E03-AA")

 

or

 

("CC = EE03-A")

 

or might you ever get as far as

 

("CCC = E03-A")

 

or who-knows-what-other possibilities?  If so, that raw string length comparison could throw off the results.

Kent Cooper, AIA
0 Likes
Message 5 of 5

msarqui
Collaborator
Collaborator
It is also working Kent.
I will let you both know if I have future issues with this.
Many thanks!
0 Likes