Author Topic: Search of Windows type Sort function  (Read 30287 times)

0 Members and 1 Guest are viewing this topic.

antistar

  • Guest
Re: Search of Windows type Sort function
« Reply #15 on: January 28, 2014, 06:39:59 AM »
You might look into DOSLib's DOS_STRSORT function and it's logical sorting option.

http://www.en.na.mcneel.com/doslib/string_functions/dos_strsort.htm

For example:

Command: (dos_strsort '("A1" "A10" "A9" "B05" "B11" "B2") -1)
("A1" "A9" "A10" "B2" "B05" "B11")

Does this help?

-- Dale

Hi Dale,
This function does not return what I need.
Still I appreciate your attention and reply.
Thanks a lot...  :-)

antistar

  • Guest
Re: Search of Windows type Sort function
« Reply #16 on: January 28, 2014, 06:44:23 AM »

I have this:

Code: [Select]
(ALE_List_SortAtoms '("T1A" "AS2" "AS10" "T10B" "T1" "AS1" "T2" "T10" "T2A" "T10A" "T1B" "T2B"))
=====>  ("AS1" "AS2" "AS10" "T1" "T2" "T10" "T10A" "T10B" "T1A" "T2A" "T1B" "T2B")
Your=>  ("AS1" "AS2" "AS10" "T1" "T2" "T10" "T1A" "T2A" "T10A" "T1B" "T2B" "T10B")

Hi Marc'Antonio,
Your code is what is closer than I need.
Thanks for your attention and help.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Search of Windows type Sort function
« Reply #17 on: January 28, 2014, 08:49:37 AM »
You can use Gile's Alpha2Num routine & index sort on the resulting numbers.  8)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Search of Windows type Sort function
« Reply #18 on: January 28, 2014, 07:13:48 PM »
I have this in my library:

Code - Auto/Visual Lisp: [Select]
  1. ;; Alphanumerical Sort  -  Lee Mac
  2. ;; Sorts a list of strings containing a combination of alphabetical & numerical characters.
  3.  
  4. (defun LM:alphanumsort ( lst )
  5.     (mapcar (function (lambda ( n ) (nth n lst)))
  6.         (vl-sort-i (mapcar 'LM:splitstring lst)
  7.             (function
  8.                 (lambda ( a b / x y )
  9.                     (while
  10.                         (and
  11.                             (setq x (car a))
  12.                             (setq y (car b))
  13.                             (= x y)
  14.                         )
  15.                         (setq a (cdr a)
  16.                               b (cdr b)
  17.                         )
  18.                     )
  19.                     (cond
  20.                         (   (null x) b)
  21.                         (   (null y) nil)
  22.                         (   (and (numberp x) (numberp y)) (< x y))
  23.                         (   (numberp x))
  24.                         (   (numberp y) nil)
  25.                         (   (< x y))
  26.                     )
  27.                 )
  28.             )
  29.         )
  30.     )
  31. )
  32.  
  33. ;; Split String  -  Lee Mac
  34. ;; Splits a string into a list of text and numbers
  35.  
  36. (defun LM:splitstring ( str )
  37.     (
  38.         (lambda ( l )
  39.             (read
  40.                 (strcat "("
  41.                     (vl-list->string
  42.                         (apply 'append
  43.                             (mapcar
  44.                                 (function
  45.                                     (lambda ( a b c )
  46.                                         (cond
  47.                                             (   (= 92 b)
  48.                                                 (list 32 34 92 b 34 32)
  49.                                             )
  50.                                             (   (or (< 47 b 58)
  51.                                                     (and (= 45 b) (< 47 c 58) (not (< 47 a 58)))
  52.                                                     (and (= 46 b) (< 47 a 58) (< 47 c 58))
  53.                                                 )
  54.                                                 (list b)
  55.                                             )
  56.                                             (   (list 32 34 b 34 32))
  57.                                         )
  58.                                     )
  59.                                 )
  60.                                 (cons nil l) l (append (cdr l) '(( )))
  61.                             )
  62.                         )
  63.                     )
  64.                     ")"
  65.                 )
  66.             )
  67.         )
  68.         (vl-string->list str)
  69.     )
  70. )

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Search of Windows type Sort function
« Reply #19 on: January 29, 2014, 03:01:43 AM »
I have this in my library:...
Hi Lee,
Code: [Select]
ALE=====>  ("AS1" "AS2" "AS10" "T1" "T2" "T10" "T10A" "T10B" "T1A" "T2A" "T1B" "T2B")
LM:=====>  ("AS1" "AS2" "AS10" "T1" "T1A" "T1B" "T2" "T2A" "T2B" "T10" "T10A" "T10B")
Antistar>  ("AS1" "AS2" "AS10" "T1" "T2" "T10" "T1A" "T2A" "T10A" "T1B" "T2B" "T10B")
which is more correct?

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: Search of Windows type Sort function
« Reply #20 on: January 29, 2014, 06:45:39 AM »
It seems from a logic perspective that LeeMac's is more correct.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Search of Windows type Sort function
« Reply #21 on: January 30, 2014, 10:58:49 AM »
My attempt resulted same as Lee but not so elegant.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ()
  2.   (MySort '("T2A" "AS10" "T10B" "T1" "T2" "AS2" "T10" "T1A" "AS1" "T10A" "T1B" "T2B"))
  3. ;|
  4.   ALE=====>  ("AS1" "AS2" "AS10" "T1" "T2" "T10" "T10A" "T10B" "T1A" "T2A" "T1B" "T2B")
  5.   LM:=====>  ("AS1" "AS2" "AS10" "T1" "T1A" "T1B" "T2" "T2A" "T2B" "T10" "T10A" "T10B")
  6.   Antistar>  ("AS1" "AS2" "AS10" "T1" "T2" "T10" "T1A" "T2A" "T10A" "T1B" "T2B" "T10B") desired result
  7.   CAB=====>  ("AS1" "AS2" "AS10" "T1" "T1A" "T1B" "T2" "T2A" "T2B" "T10" "T10A" "T10B")
  8.              
  9. |;
  10.   )
  11.  
  12. ;;  CAB 01/30/14
  13. (defun parseNum (str / lst tnum tstr)
  14.   (mapcar
  15.     (function
  16.      (lambda(x)
  17.        (cond
  18.          ((< 47 x 58) ; number
  19.           (if tstr (setq lst (cons (vl-list->string (reverse tstr)) lst) tstr nil))
  20.           (if tnum (setq tnum (cons x tnum))(setq tnum (list x)))
  21.           )
  22.          (t ; non-number
  23.           (if tnum (setq lst (cons (atoi(vl-list->string (reverse tnum))) lst) tnum nil))
  24.           (if tstr (setq tstr (cons x tstr))(setq tstr (list x)))
  25.           )
  26.         )
  27.        ))
  28.     (vl-string->list str))
  29.   (if tstr (setq lst (cons (vl-list->string (reverse tstr)) lst)))
  30.   (if tnum (setq lst (cons (atoi(vl-list->string (reverse tnum))) lst)))
  31.   (reverse lst)
  32. )
  33.  
  34.  
  35. (defun MySort (lst)
  36.   (mapcar '(lambda (x) (nth x lst))
  37.           (vl-sort-i (mapcar '(lambda(x) (ParseNum x)) lst)
  38.                      '(lambda (e1 e2)
  39.                         (if (= (car e1) (car e2))
  40.                           (if (= (cadr e1) (cadr e2))
  41.                             (< (caddr e1) (caddr e2))
  42.                             (< (cadr e1) (cadr e2))
  43.                           )
  44.                           (< (car e1) (car e2))
  45.                         )
  46.                       )
  47.           )
  48.   )
  49. )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Search of Windows type Sort function
« Reply #22 on: June 27, 2017, 01:48:46 PM »
100 kudos  :smitten:
Keep smile...

rayakmal

  • Newt
  • Posts: 49
Re: Search of Windows type Sort function
« Reply #23 on: September 24, 2018, 04:51:26 AM »
I have this in my library:

Code - Auto/Visual Lisp: [Select]
  1. ;; Alphanumerical Sort  -  Lee Mac
  2. ;; Sorts a list of strings containing a combination of alphabetical & numerical characters.
  3.  
  4. (defun LM:alphanumsort ( lst )
  5.     (mapcar (function (lambda ( n ) (nth n lst)))
  6.         (vl-sort-i (mapcar 'LM:splitstring lst)
  7.             (function
  8.                 (lambda ( a b / x y )
  9.                     (while
  10.                         (and
  11.                             (setq x (car a))
  12.                             (setq y (car b))
  13.                             (= x y)
  14.                         )
  15.                         (setq a (cdr a)
  16.                               b (cdr b)
  17.                         )
  18.                     )
  19.                     (cond
  20.                         (   (null x) b)
  21.                         (   (null y) nil)
  22.                         (   (and (numberp x) (numberp y)) (< x y))
  23.                         (   (numberp x))
  24.                         (   (numberp y) nil)
  25.                         (   (< x y))
  26.                     )
  27.                 )
  28.             )
  29.         )
  30.     )
  31. )
  32.  
  33. ;; Split String  -  Lee Mac
  34. ;; Splits a string into a list of text and numbers
  35.  
  36. (defun LM:splitstring ( str )
  37.     (
  38.         (lambda ( l )
  39.             (read
  40.                 (strcat "("
  41.                     (vl-list->string
  42.                         (apply 'append
  43.                             (mapcar
  44.                                 (function
  45.                                     (lambda ( a b c )
  46.                                         (cond
  47.                                             (   (= 92 b)
  48.                                                 (list 32 34 92 b 34 32)
  49.                                             )
  50.                                             (   (or (< 47 b 58)
  51.                                                     (and (= 45 b) (< 47 c 58) (not (< 47 a 58)))
  52.                                                     (and (= 46 b) (< 47 a 58) (< 47 c 58))
  53.                                                 )
  54.                                                 (list b)
  55.                                             )
  56.                                             (   (list 32 34 b 34 32))
  57.                                         )
  58.                                     )
  59.                                 )
  60.                                 (cons nil l) l (append (cdr l) '(( )))
  61.                             )
  62.                         )
  63.                     )
  64.                     ")"
  65.                 )
  66.             )
  67.         )
  68.         (vl-string->list str)
  69.     )
  70. )

What if I have a multi dimension list like this:

(("A.05" "12.34" "Regular") ("A.10 "34.54 "BigSize") ("B.9" "66.73" "Regular") ("A.05" "12.55" "BigSize"))

What is the most efficient and correct way  to sort this list?

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Search of Windows type Sort function
« Reply #24 on: September 24, 2018, 12:28:27 PM »
What if I have a multi dimension list like this:

(("A.05" "12.34" "Regular") ("A.10 "34.54 "BigSize") ("B.9" "66.73" "Regular") ("A.05" "12.55" "BigSize"))

What is the most efficient and correct way  to sort this list?

Should the list be sorted by the first element; or by first, then second, then third?

rayakmal

  • Newt
  • Posts: 49
Re: Search of Windows type Sort function
« Reply #25 on: April 29, 2019, 12:21:22 AM »
What if I have a multi dimension list like this:

(("A.05" "12.34" "Regular") ("A.10 "34.54 "BigSize") ("B.9" "66.73" "Regular") ("A.05" "12.55" "BigSize"))

What is the most efficient and correct way  to sort this list?

Should the list be sorted by the first element; or by first, then second, then third?

I gave a wrong list. It should be like this:

(("A.05" 12.34 "Regular") ("A.10" 34.54 "BigSize") ("B.9" 66.73 "Regular") ("A.10" 12.12 "BigSize") ("A.05" 12.55 "BigSize"))

The result:
(("A.05" 12.34 "Regular") ("A.05" 12.55 "BigSize") ("A.10" 12.12 "BigSize") ("A.10" 34.54 "BigSize") ("B.9" 66.73 "Regular"))
The list should be sort by the first element and then second.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Search of Windows type Sort function
« Reply #26 on: April 29, 2019, 08:19:37 AM »
I gave a wrong list. It should be like this:
Code: [Select]
(("A.05" 12.34 "Regular") ("A.10" 34.54 "BigSize") ("B.9" 66.73 "Regular") ("A.10" 12.12 "BigSize") ("A.05" 12.55 "BigSize"))The result:
Code: [Select]
(("A.05" 12.34 "Regular") ("A.05" 12.55 "BigSize") ("A.10" 12.12 "BigSize") ("A.10" 34.54 "BigSize") ("B.9" 66.73 "Regular"))The list should be sort by the first element and then second.

Consider the following function based on the code from my earlier post -
Code - Auto/Visual Lisp: [Select]
  1. (defun mysort ( l )
  2.     (vl-sort l
  3.         (function
  4.             (lambda ( a b / x y )
  5.                 (if (= (car  a) (car  b))
  6.                     (< (cadr a) (cadr b))
  7.                     (progn
  8.                         (setq a (LM:splitstring (car a))
  9.                               b (LM:splitstring (car b))
  10.                         )
  11.                         (while
  12.                             (and
  13.                                 (setq x (car a))
  14.                                 (setq y (car b))
  15.                                 (= x y)
  16.                             )
  17.                             (setq a (cdr a)
  18.                                   b (cdr b)
  19.                             )
  20.                         )
  21.                         (cond
  22.                             (   (null x) b)
  23.                             (   (null y) nil)
  24.                             (   (and (numberp x) (numberp y)) (< x y))
  25.                             (   (numberp x))
  26.                             (   (numberp y) nil)
  27.                             (   (< x y))
  28.                         )
  29.                     )
  30.                 )
  31.             )
  32.         )
  33.     )
  34. )
  35.  
  36. ;; Split String  -  Lee Mac
  37. ;; Splits a string into a list of text and numbers
  38.  
  39. (defun LM:splitstring ( str )
  40.     (
  41.         (lambda ( l )
  42.             (read
  43.                 (strcat "("
  44.                     (vl-list->string
  45.                         (apply 'append
  46.                             (mapcar
  47.                                 (function
  48.                                     (lambda ( a b c )
  49.                                         (cond
  50.                                             (   (= 92 b)
  51.                                                 (list 32 34 92 b 34 32)
  52.                                             )
  53.                                             (   (or (< 47 b 58)
  54.                                                     (and (= 45 b) (< 47 c 58) (not (< 47 a 58)))
  55.                                                     (and (= 46 b) (< 47 a 58) (< 47 c 58))
  56.                                                 )
  57.                                                 (list b)
  58.                                             )
  59.                                             (   (list 32 34 b 34 32))
  60.                                         )
  61.                                     )
  62.                                 )
  63.                                 (cons nil l) l (append (cdr l) '(( )))
  64.                             )
  65.                         )
  66.                     )
  67.                     ")"
  68.                 )
  69.             )
  70.         )
  71.         (vl-string->list str)
  72.     )
  73. )

Example:
Code - Auto/Visual Lisp: [Select]
  1. _$ (mysort '(("A.05" 12.34 "Regular") ("A.10" 34.54 "BigSize") ("B.9" 66.73 "Regular") ("A.10" 12.12 "BigSize") ("A.05" 12.55 "BigSize")))
  2. (("A.05" 12.34 "Regular") ("A.05" 12.55 "BigSize") ("A.10" 12.12 "BigSize") ("A.10" 34.54 "BigSize") ("B.9" 66.73 "Regular"))


ronjonp

  • Needs a day job
  • Posts: 7526
Re: Search of Windows type Sort function
« Reply #27 on: April 29, 2019, 12:37:24 PM »
I'm sure this logic is flawed and horribly inefficient but it's the first thing that came to mind :)
Code - Auto/Visual Lisp: [Select]
  1. (vl-sort '(("A.05" 12.34 "Regular")
  2.            ("A.10" 34.54 "BigSize")
  3.            ("B.9" 66.73 "Regular")
  4.            ("A.10" 12.12 "BigSize")
  5.            ("A.05" 12.55 "BigSize")
  6.           )
  7.          '(lambda (a b)
  8.             (< (apply 'strcat (mapcar 'vl-princ-to-string a))
  9.                (apply 'strcat (mapcar 'vl-princ-to-string b))
  10.             )
  11.           )
  12. )
  13. ;; (("A.05" 12.34 "Regular") ("A.05" 12.55 "BigSize") ("A.10" 12.12 "BigSize") ("A.10" 34.54 "BigSize") ("B.9" 66.73 "Regular"))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Search of Windows type Sort function
« Reply #28 on: April 29, 2019, 12:59:54 PM »
I'm sure this logic is flawed and horribly inefficient but it's the first thing that came to mind :)

Add ("B.10" 66.73 "Regular") to your list  :wink:

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Search of Windows type Sort function
« Reply #29 on: April 29, 2019, 03:34:23 PM »
I'm sure this logic is flawed and horribly inefficient but it's the first thing that came to mind :)

Add ("B.10" 66.73 "Regular") to your list  :wink:
DOH!   :-) That's some strange numbering since "A.05" is formatted correctly.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC