See the top rated post in this thread. Click here

Results 1 to 3 of 3

Thread: Sorting List either Alpha/Numeric or combined

  1. #1
    100 Club
    Join Date
    2000-11
    Posts
    136
    Login to Give a bone
    0

    Default Sorting List either Alpha/Numeric or combined

    I had a hard time trying to get revision numbers sorted when some dwgs were alpha and others numeric and some mixed. I read about getting the MAX from a list and other things. Something always failed but being old and stubborn I kept beating on the keyboard. Plus some more surfing in the forums. Finally through dumb luck I found one simple routine that worked great. The first character of each revision has to be either numeric or alpha and this works beautifully. I thought I would post it along with a couple of lines I used for testing. Doesn't matter where in the list the largest value is it determines it. Just wanted to share this and maybe save someone else from struggling to sort some list.

    (setq
    kissr1 "0"
    kissr2 "1B"
    kissr3 "1A"
    kissr4 "4A"
    kissr5 "4"
    kissr6 "6A"
    kissr7 "6"
    kissr8 "1"
    )

    (setq kissrev2sort (list kissr1 kissr2 kissr3 kissr4 kissr5 kissr6 kissr7 kissr)
    (setq KISSMRV (strcase (last (acad_strlsort kissrev2sort))))
    (princ (strcat "KISSMRV = " KISSMRV))



    (setq
    kissr1 "a"
    kissr2 "b"
    kissr3 "B1"
    kissr4 "C"
    kissr5 "C3"
    kissr6 "C2"
    kissr7 ""
    kissr8 ""
    )

  2. #2
    All AUGI, all the time marko_ribar's Avatar
    Join Date
    2015-10
    Location
    Belgrade, Serbia, Europe
    Posts
    539
    Login to Give a bone
    0

    Default Re: Sorting List either Alpha/Numeric or combined

    Here is topic to review - there are some interesting ways of sorting by first element of sublists, but you can use it to sort your main list the same way by your string elements - just simple mod. to functions [(car a) (car b)] => [a b]...

    https://www.cadtutor.net/forum/topic...he-inner-list/

    HTH., M.R.
    Marko Ribar, d.i.a. (graduated engineer of architecture)

    M.R. on Youtube


  3. #3
    All AUGI, all the time marko_ribar's Avatar
    Join Date
    2015-10
    Location
    Belgrade, Serbia, Europe
    Posts
    539
    Login to Give a bone
    1

    Default Re: Sorting List either Alpha/Numeric or combined

    Maybe, just simply :

    Code:
    (defun _sort-1 ( l / asciivalue )
    
      (defun asciivalue ( str / s sl sv )
        (while (/= "" (setq s (substr str 1 1)))
          (setq sl (cons s sl))
          (setq str (substr str 2))
        )
        (setq sl (reverse sl))
        (setq sv (apply 'strcat (mapcar '(lambda ( x ) (itoa (+ 100 (ascii x)))) sl)))
        sv
      )
    
      (vl-sort l '(lambda ( a b ) (< (asciivalue a) (asciivalue b))))
    )
    
    (defun _sort-2 ( l / groupbystrlen )
    
      (defun groupbystrlen ( l / nmax k g gg )
        (setq nmax (strlen (car (vl-sort l '(lambda ( a b ) (> (strlen a) (strlen b)))))))
        (setq k 0)
        (repeat nmax
          (setq k (1+ k))
          (setq g (vl-remove-if-not '(lambda ( x ) (= (strlen x) k)) l))
          (setq gg (cons g gg))
        )
        (reverse gg)
      )
    
      (apply 'append (mapcar '(lambda ( x ) (acad_strlsort x)) (groupbystrlen l)))
    )
    
    (defun _sort-3 ( l / groupbystrlen )
    
      (defun groupbystrlen ( l / nmax k g gg )
        (setq nmax (strlen (car (vl-sort l '(lambda ( a b ) (> (strlen a) (strlen b)))))))
        (setq k 0)
        (repeat nmax
          (setq k (1+ k))
          (setq g (vl-remove-if-not '(lambda ( x ) (= (strlen x) k)) l))
          (setq gg (cons g gg))
        )
        (reverse gg)
      )
    
      (apply 'append (mapcar '(lambda ( x ) (vl-sort x '<)) (groupbystrlen l)))
    )
    
    (defun _sort-4 ( l / groupbystrlen asciivalue gg nmax q gav gsav ggsav )
    
      (defun groupbystrlen ( l / nmax k g gg )
        (setq nmax (strlen (car (vl-sort l '(lambda ( a b ) (> (strlen a) (strlen b)))))))
        (setq k 0)
        (repeat nmax
          (setq k (1+ k))
          (setq g (vl-remove-if-not '(lambda ( x ) (= (strlen x) k)) l))
          (setq gg (cons g gg))
        )
        (reverse gg)
      )
    
      (defun asciivalue ( str / s sl sv )
        (while (/= "" (setq s (substr str 1 1)))
          (setq sl (cons s sl))
          (setq str (substr str 2))
        )
        (setq sl (reverse sl))
        (setq sv (apply 'strcat (mapcar '(lambda ( x ) (itoa (+ 100 (ascii x)))) sl)))
        sv
      )
    
      (setq gg (groupbystrlen l))
      (setq nmax (strlen (car (last gg))))
      (foreach g gg
        (setq q "")
        (repeat (- nmax (strlen (car g)))
          (setq q (strcat "099" q))
        )
        (setq gav (mapcar '(lambda ( x ) (strcat (asciivalue x) q)) g))
        (setq gsav (mapcar '(lambda ( a b ) (list a b)) g gav))
        (setq ggsav (cons gsav ggsav))
      )
      (setq ggsav (apply 'append (reverse ggsav)))
      (mapcar 'car (vl-sort ggsav '(lambda ( a b ) (< (cadr a) (cadr b)))))
    )
    
    ;; Alphanumerical Sort  -  Lee Mac
    ;; Sorts a list of strings containing a combination of alphabetical & numerical characters.
    
    (defun LM:AlphanumSort ( l )
      (mapcar (function (lambda ( n ) (nth n l)))
        (vl-sort-i (mapcar 'LM:SplitString l)
          (function
            (lambda ( a b )
              ( (lambda ( f ) (f a b))
                (lambda ( a b / x y )
                  (setq x (car a) y (car b))
                  (cond
                    ( (null x) b)
                    ( (null y) nil)
                    ( (= x y) (f (cdr a) (cdr b)))
                    ( (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 ( s )
      (
        (lambda ( l )
          (read
            (strcat "("
              (vl-list->string
                (apply 'append
                  (mapcar
                    (function
                      (lambda ( a b c )
                        (if (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 s)
      )
    )
    
    ;|
    (setq l (list "0" "01" "010" "020" "0a0" "0A0" "0c0" "0C0" "0a1" "0c1" "012" "01a" "02a" "1" "11" "110" "120" "1a0" "1A0" "1c0" "1C0" "1a1" "1c1" "112" "11a" "12a" "a" "a1" "a10" "a20" "aa0" "aA0" "ac0" "aC0" "aa1" "ac1" "a12" "a1a" "a2a" "c" "c1" "c10" "c20" "ca0" "cA0" "cc0" "cC0" "ca1" "cc1" "c12" "c1a" "c2a" "A" "A1" "A10" "A20" "Aa0" "AA0" "Ac0" "AC0" "Aa1" "Ac1" "A12" "A1a" "A2a" "C" "C1" "C10" "C20" "Ca0" "CA0" "Cc0" "CC0" "Ca1" "Cc1" "C12" "C1a" "C2a"))
    
    (_sort-1 l) => ("0" "1" "A" "C" "a" "c" "01" "11" "A1" "C1" "a1" "c1" "010" "012" "01a" "020" "02a" "0A0" "0C0" "0a0" "0a1" "0c0" "0c1" "110" "112" "11a" "120" "12a" "1A0" "1C0" "1a0" "1a1" "1c0" "1c1" "A10" "A12" "A1a" "A20" "A2a" "AA0" "AC0" "Aa0" "Aa1" "Ac0" "Ac1" "C10" "C12" "C1a" "C20" "C2a" "CA0" "CC0" "Ca0" "Ca1" "Cc0" "Cc1" "a10" "a12" "a1a" "a20" "a2a" "aA0" "aC0" "aa0" "aa1" "ac0" "ac1" "c10" "c12" "c1a" "c20" "c2a" "cA0" "cC0" "ca0" "ca1" "cc0" "cc1")
    
    (_sort-2 l) => ("0" "1" "a" "A" "c" "C" "01" "11" "a1" "A1" "c1" "C1" "010" "012" "01a" "020" "02a" "0a0" "0A0" "0a1" "0c0" "0C0" "0c1" "110" "112" "11a" "120" "12a" "1a0" "1A0" "1a1" "1c0" "1C0" "1c1" "a10" "A10" "a12" "A12" "a1a" "A1a" "a20" "A20" "a2a" "A2a" "aa0" "aA0" "Aa0" "AA0" "aa1" "Aa1" "ac0" "aC0" "Ac0" "AC0" "ac1" "Ac1" "c10" "C10" "c12" "C12" "c1a" "C1a" "c20" "C20" "c2a" "C2a" "ca0" "cA0" "Ca0" "CA0" "ca1" "Ca1" "cc0" "cC0" "Cc0" "CC0" "cc1" "Cc1")
    
    (_sort-3 l) => ("0" "1" "A" "C" "a" "c" "01" "11" "A1" "C1" "a1" "c1" "010" "012" "01a" "020" "02a" "0A0" "0C0" "0a0" "0a1" "0c0" "0c1" "110" "112" "11a" "120" "12a" "1A0" "1C0" "1a0" "1a1" "1c0" "1c1" "A10" "A12" "A1a" "A20" "A2a" "AA0" "AC0" "Aa0" "Aa1" "Ac0" "Ac1" "C10" "C12" "C1a" "C20" "C2a" "CA0" "CC0" "Ca0" "Ca1" "Cc0" "Cc1" "a10" "a12" "a1a" "a20" "a2a" "aA0" "aC0" "aa0" "aa1" "ac0" "ac1" "c10" "c12" "c1a" "c20" "c2a" "cA0" "cC0" "ca0" "ca1" "cc0" "cc1")
    
    (_sort-1 l) = (_sort-3 l) => (vl-sort l '<) is sorting according to ascii values
    
    (_sort-4 l) => ("0" "01" "010" "012" "01a" "020" "02a" "0A0" "0C0" "0a0" "0a1" "0c0" "0c1" "1" "11" "110" "112" "11a" "120" "12a" "1A0" "1C0" "1a0" "1a1" "1c0" "1c1" "A" "A1" "A10" "A12" "A1a" "A20" "A2a" "AA0" "AC0" "Aa0" "Aa1" "Ac0" "Ac1" "C" "C1" "C10" "C12" "C1a" "C20" "C2a" "CA0" "CC0" "Ca0" "Ca1" "Cc0" "Cc1" "a" "a1" "a10" "a12" "a1a" "a20" "a2a" "aA0" "aC0" "aa0" "aa1" "ac0" "ac1" "c" "c1" "c10" "c12" "c1a" "c20" "c2a" "cA0" "cC0" "ca0" "ca1" "cc0" "cc1")
    
    (LM:AlphanumSort l) => ("0" "0A0" "0C0" "0a0" "0a1" "0c0" "0c1" "1" "01" "1A0" "1C0" "01a" "1a0" "1a1" "1c0" "1c1" "02a" "010" "11" "11a" "012" "12a" "020" "110" "112" "120" "A" "A1" "A1a" "A2a" "A10" "A12" "A20" "AA0" "AC0" "Aa0" "Aa1" "Ac0" "Ac1" "C" "C1" "C1a" "C2a" "C10" "C12" "C20" "CA0" "CC0" "Ca0" "Ca1" "Cc0" "Cc1" "a" "a1" "a1a" "a2a" "a10" "a12" "a20" "aA0" "aC0" "aa0" "aa1" "ac0" "ac1" "c" "c1" "c1a" "c2a" "c10" "c12" "c20" "cA0" "cC0" "ca0" "ca1" "cc0" "cc1")
    
    (acad_strlsort l) => ("0" "01" "010" "012" "01a" "020" "02a" "0a0" "0A0" "0a1" "0c0" "0C0" "0c1" "1" "11" "110" "112" "11a" "120" "12a" "1a0" "1A0" "1a1" "1c0" "1C0" "1c1" "a" "A" "a1" "A1" "a10" "A10" "a12" "A12" "a1a" "A1a" "a20" "A20" "a2a" "A2a" "aa0" "aA0" "Aa0" "AA0" "aa1" "Aa1" "ac0" "aC0" "Ac0" "AC0" "ac1" "Ac1" "c" "C" "c1" "C1" "c10" "C10" "c12" "C12" "c1a" "C1a" "c20" "C20" "c2a" "C2a" "ca0" "cA0" "Ca0" "CA0" "ca1" "Ca1" "cc0" "cC0" "Cc0" "CC0" "cc1" "Cc1")
    
    (vl-sort l '<) => ("0" "01" "010" "012" "01a" "020" "02a" "0A0" "0C0" "0a0" "0a1" "0c0" "0c1" "1" "11" "110" "112" "11a" "120" "12a" "1A0" "1C0" "1a0" "1a1" "1c0" "1c1" "A" "A1" "A10" "A12" "A1a" "A20" "A2a" "AA0" "AC0" "Aa0" "Aa1" "Ac0" "Ac1" "C" "C1" "C10" "C12" "C1a" "C20" "C2a" "CA0" "CC0" "Ca0" "Ca1" "Cc0" "Cc1" "a" "a1" "a10" "a12" "a1a" "a20" "a2a" "aA0" "aC0" "aa0" "aa1" "ac0" "ac1" "c" "c1" "c10" "c12" "c1a" "c20" "c2a" "cA0" "cC0" "ca0" "ca1" "cc0" "cc1")
    
    (_sort-4 l) = (vl-sort l '<) => (vl-sort l '<) is sorting according to ascii values
    |;
    M.R.
    Last edited by marko_ribar; 2019-03-18 at 03:29 AM.
    Marko Ribar, d.i.a. (graduated engineer of architecture)

    M.R. on Youtube


Similar Threads

  1. Replies: 0
    Last Post: 2010-09-21, 02:06 PM
  2. Alpha Numeric room tags
    By mrgeee92329 in forum ACA General
    Replies: 4
    Last Post: 2008-12-04, 12:20 PM
  3. QSELECT Numeric and alpha text
    By Shoey in forum AutoCAD General
    Replies: 4
    Last Post: 2008-08-15, 09:17 PM
  4. Another way to check alpha vs. numeric?
    By jguest in forum AutoLISP
    Replies: 11
    Last Post: 2006-04-25, 10:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •