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

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Search of Windows type Sort function
« Reply #30 on: April 29, 2019, 06:03:46 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.

I agree - if leading zeroes could be guaranteed, this task would be significantly easier...  :-)

rayakmal

  • Newt
  • Posts: 49
Re: Search of Windows type Sort function
« Reply #31 on: April 29, 2019, 09:56:29 PM »

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"))

Wow  :-o :-o :-o That's what I want, Lee. 
Once again, Thanks.  I really appreciate your help.

Ron, Thanks for chiming in.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Search of Windows type Sort function
« Reply #32 on: May 01, 2019, 01:58:25 PM »
You're welcome!  :-)