Author Topic: Help in Sorting Lists of Numbers  (Read 1308 times)

0 Members and 1 Guest are viewing this topic.

Ambo

  • Mosquito
  • Posts: 20
Help in Sorting Lists of Numbers
« on: October 04, 2020, 06:48:58 AM »
Hello Cad Gurus,

I have to sort lists of numbers in a lot of drawings in multiple tabs and those numbers can be found in texts, mtexts or attributes. The lists are quite long and would take me ages to sort them in order. Any help with the gurus will be highly appreciated.

Examples of a string with numbers and symbols to be sorted numerically:
3 2 4 5 1                   to be   1, 2, 3, 4 & 5
3, 2, 4, 5 & 1             to be   1, 2, 3, 4 & 5     
3(a) 2(b) 4(c) 1 5      to be   1, 2(b), 3(a), 4(c) & 5
2 3-5 1                     to be   1, 2 & 3-5

Commas between numbers and the '&' symbol should be added before the last number on the list while keeping the number in range together if present (like 3-5) just like the last example above.

Thank you.

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: Help in Sorting Lists of Numbers
« Reply #1 on: October 04, 2020, 07:42:33 PM »
Vl-sort would do the 1st two 1 2 3 4 5 etc the csv just make a list so long as just numbers.

Many years ago wrote BUbble-sort in lisp you may need something like that rather than vl-sort.

Looking at excel must sort a mixture 22 3 4(b) 2(a) all as strings not a mix.

Code: [Select]
(setq lst '("z" "2(a)" "34" "4(b)"))
("z" "2(a)" "34" "4(b)")

Command: (vl-sort lst '<)
("2(a)" "34" "4(b)" "z")

A man who never made a mistake never made anything

kpblc

  • Bull Frog
  • Posts: 396
Re: Help in Sorting Lists of Numbers
« Reply #2 on: October 05, 2020, 02:21:26 PM »
Code - Auto/Visual Lisp: [Select]
  1. (setq lst '("3""2""4""5""1"))
  2. (vl-sort lst (function (lambda (a b) (< (atof a) (atof b)))))
:?:
Sorry for my English.

Ambo

  • Mosquito
  • Posts: 20
Re: Help in Sorting Lists of Numbers
« Reply #3 on: October 05, 2020, 02:55:28 PM »
Big thanks BIGAL & kpblc!

I tried what you've suggested and that took care of the sorting function. All I need to do now is to learn or find out out how to create the list from a selection and then the big question of how to put them back together with the commas and '&' symbol before the last number.  I would always welcome some suggestions and samples to finish to job. 


BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: Help in Sorting Lists of Numbers
« Reply #4 on: October 05, 2020, 10:17:59 PM »
List manipulation so its easy to do what you want converting to a comma delimeter string just need to use a strcat and a repeat that does a (- (length lst) 1) add "," then strcat last item in list.

Add the & is the same just need to step through the list then add in & and last item.

It would be best to supply some lists '("a" "5" "2(a)") etc and desired string rather than have 2 or 3 goes at the code.

A quick example

Code: [Select]
; numbers to csv
; can use type to check nth x lst is string, int or real
(setq lst '( 1 2 3 4 5 6))
(setq x 0 str "")

(repeat (- (length lst) 1)
   (setq str (strcat str (rtos (nth x lst) 2 0) ","))
  (setq x (+ x 1))
)

(setq str (strcat str (rtos (nth x lst) 2 0)))

A man who never made a mistake never made anything

Ambo

  • Mosquito
  • Posts: 20
Re: Help in Sorting Lists of Numbers
« Reply #5 on: October 07, 2020, 05:13:50 PM »
Thanks again Bigal.