AutoCAD LT Count Table Sorting

AutoCAD LT Count Table Sorting

d_salesU4WW6
Contributor Contributor
1,035 Views
15 Replies
Message 1 of 16

AutoCAD LT Count Table Sorting

d_salesU4WW6
Contributor
Contributor

Good Afternoon,

 

We use count tables for our Legends but currently it sorts by alphabetical order - is there a way to manage the order of the rows in the table? I don't mind whether its a drag and drop I do each time or whether its set into the background of the table.

 

Many thanks

 

0 Likes
Accepted solutions (1)
1,036 Views
15 Replies
Replies (15)
Message 2 of 16

h_s_walker
Mentor
Mentor

I've not downloaded it, but you can try the app in the link below

Table Sort | AutoCAD | Autodesk App Store

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 3 of 16

paullimapa
Mentor
Mentor

FYI: since @d_salesU4WW6 is running LT, the app which uses arx to load will most likely not load on LT.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 4 of 16

h_s_walker
Mentor
Mentor

@paullimapa Yeah you're right. I just tried.

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 5 of 16

h_s_walker
Mentor
Mentor

@d_salesU4WW6 I've just been working with ChatGPT and have the following lisp for it. It will ask for a column to sort by (0 is the first column). It will recognise if it's letters or numbers and will then ask if you want to sort A-Z or Z-A or 1-9 or 9-1. See the image at the end for the results.

The only problem that ChatGPT pointed out is LT cannot write fields in a lisp, so it converts all the fields to text.

(defun isNumber (s)
  (not (null (distof s 2)))
)

(defun c:SortTableByCol ( / ent obj rows cols data colIndex r c rowData sorted numeric order )

  (vl-load-com)

  ;; Select table
  (setq ent (car (entsel "\nSelect table: ")))
  (setq obj (vlax-ename->vla-object ent))

  (setq rows (vla-get-Rows obj))
  (setq cols (vla-get-Columns obj))

  ;; --- SAFE COLUMN INPUT ---
  (setq colIndex nil)
  (while (not (and colIndex (>= colIndex 0) (< colIndex cols)))
    (setq colIndex
      (getint
        (strcat "\nEnter column index (0-" (itoa (1- cols)) "): ")
      )
    )
    (if (null colIndex)
      (prompt "\nInvalid input. Please enter a number.")
    )
  )

  ;; Extract data (skip header row)
  (setq data '()
        r 1)

  (while (< r rows)
    (setq rowData '()
          c 0)
    (while (< c cols)
      (setq rowData (append rowData (list (vla-GetText obj r c))))
      (setq c (1+ c))
    )
    (setq data (append data (list rowData)))
    (setq r (1+ r))
  )

  ;; Detect numeric column
  (setq numeric (isNumber (nth colIndex (car data))))

  ;; Ask sort order
  (if numeric
    (progn
      (initget "1-9 9-1")
      (setq order (getkword "\nSort order [1-9/9-1] <1-9>: "))
      (if (null order) (setq order "1-9"))
    )
    (progn
      (initget "A-Z Z-A")
      (setq order (getkword "\nSort order [A-Z/Z-A] <A-Z>: "))
      (if (null order) (setq order "A-Z"))
    )
  )

  ;; Sort
  (setq sorted
    (vl-sort data
      (function
        (lambda (a b)
          (if numeric
            (if (= order "1-9")
              (< (atof (nth colIndex a)) (atof (nth colIndex b)))
              (> (atof (nth colIndex a)) (atof (nth colIndex b)))
            )
            (if (= order "A-Z")
              (< (strcase (nth colIndex a)) (strcase (nth colIndex b)))
              (> (strcase (nth colIndex a)) (strcase (nth colIndex b)))
            )
          )
        )
      )
    )
  )

  ;; Write back
  (setq r 1)
  (foreach row sorted
    (setq c 0)
    (foreach val row
      (vla-SetText obj r c val)
      (setq c (1+ c))
    )
    (setq r (1+ r))
  )

  (princ "\nTable sorted.")
  (princ)
)

 

Capture.JPG

 

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 6 of 16

h_s_walker
Mentor
Mentor

@d_salesU4WW6 You can also try this one. You need to select the table, and it will list the column number with its' header, then you choose which column and it will sort a-z/1-9, if you run the lisp again and then choose the same column it will reverse the sort order.

(setq *last-sort-col* nil)
(setq *last-sort-dir* nil)

(defun isNumber (s)
  (not (null (distof s 2)))
)

(defun c:SortTableLT ( / sel ent obj rows cols data headers r c rowData sorted numeric colIndex order valA valB headerStr)

  (vl-load-com)

  ;; --- Select table safely ---
  (setq sel nil)
  (while (not sel)
    (prompt "\nSelect table to sort: ")
    (setq sel (entsel))
    (if (null sel)
      (prompt "\nNo object selected. Try again.")
    )
  )
  (setq ent (car sel))
  (setq obj (vlax-ename->vla-object ent))

  ;; --- Table info ---
  (setq rows (vla-get-Rows obj))
  (setq cols (vla-get-Columns obj))

  ;; --- Extract header texts ---
  (setq headers '())
  (setq c 0)
  (while (< c cols)
    (setq headers (append headers (list (vla-GetText obj 0 c))))
    (setq c (1+ c))
  )

  ;; --- Show headers with indices ---
  (prompt "\nTable columns:")
  (setq c 0)
  (foreach h headers
    (prompt (strcat "\n" (itoa c) ": " h))
    (setq c (1+ c))
  )

  ;; --- Ask for column index ---
  (setq colIndex nil)
  (while (not (and colIndex (>= colIndex 0) (< colIndex cols)))
    (setq colIndex
          (getint (strcat "\nEnter column index to sort (0-" (itoa (1- cols)) "): ")))
    (if (null colIndex)
      (prompt "\nInvalid input. Please enter a number.")
    )
  )

  ;; --- Extract table data (skip header row 0) ---
  (setq data '())
  (setq r 1)
  (while (< r rows)
    (setq rowData '()
          c 0)
    (while (< c cols)
      (setq rowData (append rowData (list (vla-GetText obj r c))))
      (setq c (1+ c))
    )
    (setq data (append data (list rowData)))
    (setq r (1+ r))
  )

  ;; --- Detect numeric column ---
  (setq numeric (isNumber (nth colIndex (car data))))

  ;; --- Determine sort order (toggle if same column) ---
  (if (and (= *last-sort-col* colIndex) *last-sort-dir*)
    (setq order (if (= *last-sort-dir* "asc") "desc" "asc"))
    (setq order "asc")
  )

  ;; --- Sort data ---
  (setq sorted
        (vl-sort data
                 (function
                  (lambda (a b)
                    (setq valA (nth colIndex a))
                    (setq valB (nth colIndex b))
                    (if numeric
                      (if (= order "asc") (< (atof valA) (atof valB)) (> (atof valA) (atof valB)))
                      (if (= order "asc") (< (strcase valA) (strcase valB)) (> (strcase valA) (strcase valB)))
                    )
                  )
                 )
        )
  )

  ;; --- Write sorted data back ---
  (setq r 1)
  (foreach row sorted
    (setq c 0)
    (foreach val row
      (vla-SetText obj r c val)
      (setq c (1+ c))
    )
    (setq r (1+ r))
  )

  ;; --- Save last sorted column/direction ---
  (setq *last-sort-col* colIndex)
  (setq *last-sort-dir* order)

  (princ (strcat "\nTable sorted by column " (itoa colIndex) " (" (nth colIndex headers) ") in " order " order."))
  (princ)
)

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 7 of 16

h_s_walker
Mentor
Mentor

@d_salesU4WW6 The code below remembers the last table you selected

;; Global variables to store last table and last sort info
(setq *last-table* nil)
(setq *last-sort-col* nil)
(setq *last-sort-dir* nil)

(defun isNumber (s)
  (not (null (distof s 2)))
)

(defun c:SortTableRepeatLT ( / sel ent obj rows cols data headers r c rowData sorted numeric colIndex order valA valB)

  (vl-load-com)

  ;; --- Select table or use last table ---
  (if (and *last-table* (vlax-ename->vla-object *last-table*))
    (setq obj (vlax-ename->vla-object *last-table*))
    ;; Else select new table
    (progn
      (setq sel nil)
      (while (not sel)
        (prompt "\nSelect table to sort: ")
        (setq sel (entsel))
        (if (null sel)
          (prompt "\nNo object selected. Try again.")
        )
      )
      (setq ent (car sel))
      (setq obj (vlax-ename->vla-object ent))
      (setq *last-table* ent)
    )
  )

  ;; --- Table info ---
  (setq rows (vla-get-Rows obj))
  (setq cols (vla-get-Columns obj))

  ;; --- Extract header texts ---
  (setq headers '())
  (setq c 0)
  (while (< c cols)
    (setq headers (append headers (list (vla-GetText obj 0 c))))
    (setq c (1+ c))
  )

  ;; --- Ask for column index only if first run or new table ---
  (if (or (null *last-sort-col*) (not (eq *last-table* ent)))
    (progn
      (prompt "\nTable columns:")
      (setq c 0)
      (foreach h headers
        (prompt (strcat "\n" (itoa c) ": " h))
        (setq c (1+ c))
      )

      ;; Prompt user for column index
      (setq colIndex nil)
      (while (not (and colIndex (>= colIndex 0) (< colIndex cols)))
        (setq colIndex
              (getint (strcat "\nEnter column index to sort (0-" (itoa (1- cols)) "): ")))
        (if (null colIndex)
          (prompt "\nInvalid input. Please enter a number.")
        )
      )
    )
    ;; Use last column
    (setq colIndex *last-sort-col*)
  )

  ;; --- Extract table data (skip header row 0) ---
  (setq data '())
  (setq r 1)
  (while (< r rows)
    (setq rowData '()
          c 0)
    (while (< c cols)
      (setq rowData (append rowData (list (vla-GetText obj r c))))
      (setq c (1+ c))
    )
    (setq data (append data (list rowData)))
    (setq r (1+ r))
  )

  ;; --- Detect numeric column ---
  (setq numeric (isNumber (nth colIndex (car data))))

  ;; --- Determine sort order (toggle if same column) ---
  (if (and (= *last-sort-col* colIndex) *last-sort-dir*)
    (setq order (if (= *last-sort-dir* "asc") "desc" "asc"))
    (setq order "asc")
  )

  ;; --- Sort data ---
  (setq sorted
        (vl-sort data
                 (function
                  (lambda (a b)
                    (setq valA (nth colIndex a))
                    (setq valB (nth colIndex b))
                    (if numeric
                      (if (= order "asc") (< (atof valA) (atof valB)) (> (atof valA) (atof valB)))
                      (if (= order "asc") (< (strcase valA) (strcase valB)) (> (strcase valA) (strcase valB)))
                    )
                  )
                 )
        )
  )

  ;; --- Write sorted data back ---
  (setq r 1)
  (foreach row sorted
    (setq c 0)
    (foreach val row
      (vla-SetText obj r c val)
      (setq c (1+ c))
    )
    (setq r (1+ r))
  )

  ;; --- Save last table, column, and direction ---
  (setq *last-table* (vlax-vla-object->ename obj))
  (setq *last-sort-col* colIndex)
  (setq *last-sort-dir* order)

  (princ (strcat "\nTable sorted by column " (itoa colIndex) " (" (nth colIndex headers) ") in " order " order."))
  (princ)
)

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 8 of 16

paullimapa
Mentor
Mentor

@d_salesU4WW6 

What version of LT are you using? 

FYI: Only LT 2024 and up can load most lisp functions to run.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 9 of 16

h_s_walker
Mentor
Mentor

The lisps all work in LT2026. I tested them myself.

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 10 of 16

d_salesU4WW6
Contributor
Contributor

Hi Paul, 

 

I'm running 2026

0 Likes
Message 11 of 16

d_salesU4WW6
Contributor
Contributor

Unfortunately it needs to be a custom sort as we have CIE panels that would go above detection and interfaces would go near the bottom ect

I have attached below the way the table sorts now and how it would need to be sorted. At the moment its a manual task inserting and removing rows.

 

Screenshot 2026-04-09 090845.png

 

Screenshot 2026-04-09 091852.png

 

0 Likes
Message 12 of 16

h_s_walker
Mentor
Mentor

@d_salesU4WW6 Try this one. It asks Auto or Manual. If manual it will ask for the row order and then sort on that. EDIT: I just got ChatGPT to sort the table if it has blocks in it as well. The only problem is if the block has an attribute in it, it will lose the attribute value

Capture.JPG

(defun isNumber (s)
  (not (null (distof s 2)))
)

(defun parseOrder (str / lst pos)
  (setq lst '())
  (while (setq pos (vl-string-search "," str))
    (setq lst (cons (atoi (substr str 1 pos)) lst))
    (setq str (substr str (+ pos 2)))
  )
  (setq lst (cons (atoi str) lst))
  (reverse lst)
)

;; =========================
;; 🔹 CELL HANDLING (BLOCKS ONLY)
;; =========================

(defun getCellData (obj r c / cellType txt blk)
  (setq cellType (vla-GetCellType obj r c))
  (setq txt (vla-GetText obj r c))
  (setq blk nil)

  ;; 2 = block cell
  (if (= cellType 2)
    (setq blk (vla-GetBlockTableRecordId obj r c))
  )

  (list cellType txt blk)
)

(defun setCellData (obj r c data / cellType txt blk)
  (setq cellType (nth 0 data))
  (setq txt      (nth 1 data))
  (setq blk      (nth 2 data))

  (cond
    ;; Block cell
    ((= cellType 2)
      (vla-SetBlockTableRecordId obj r c blk :vlax-true)
    )
    ;; Text cell
    (T
      (vla-SetText obj r c txt)
    )
  )
)

;; =========================
;; 🔹 MAIN FUNCTION
;; =========================

(defun c:SortTableAdvanced ( / ent obj rows cols data colIndex r c rowData sorted numeric order mode input )

  (vl-load-com)

  ;; Select table
  (setq ent (car (entsel "\nSelect table: ")))
  (if (null ent) (exit))
  (setq obj (vlax-ename->vla-object ent))

  (setq rows (vla-get-Rows obj))
  (setq cols (vla-get-Columns obj))

  ;; Extract data (skip header row)
  (setq data '()
        r 1)

  (while (< r rows)
    (setq rowData '()
          c 0)
    (while (< c cols)
      (setq rowData (append rowData (list (getCellData obj r c))))
      (setq c (1+ c))
    )
    (setq data (append data (list rowData)))
    (setq r (1+ r))
  )

  ;; Ask mode
  (initget "Auto Manual")
  (setq mode (getkword "\nChoose mode [Auto/Manual] <Auto>: "))
  (if (null mode) (setq mode "Auto"))

  ;; =========================
  ;; 🔹 MANUAL MODE
  ;; =========================
  (if (= mode "Manual")
    (progn
      (prompt (strcat "\nNumber of data rows: " (itoa (length data))))
      (setq input (getstring "\nEnter new row order (e.g. 3,1,2,4): "))
      (setq order (parseOrder input))

      (setq r 1)
      (foreach idx order
        (if (and (>= idx 1) (<= idx (length data)))
          (progn
            (setq rowData (nth (1- idx) data))
            (setq c 0)
            (foreach cell rowData
              (setCellData obj r c cell)
              (setq c (1+ c))
            )
            (setq r (1+ r))
          )
        )
      )

      (prompt "\nTable reordered manually.")
    )

    ;; =========================
    ;; 🔹 AUTO MODE
    ;; =========================
    (progn

      ;; Column input
      (setq colIndex nil)
      (while (not (and colIndex (>= colIndex 0) (< colIndex cols)))
        (setq colIndex
          (getint
            (strcat "\nEnter column index (0-" (itoa (1- cols)) "): ")
          )
        )
        (if (null colIndex)
          (prompt "\nInvalid input. Please enter a number.")
        )
      )

      ;; Detect numeric
      (setq numeric (isNumber (nth 1 (nth colIndex (car data)))))

      ;; Sort order prompt
      (if numeric
        (progn
          (initget "1-9 9-1")
          (setq order (getkword "\nSort order [1-9/9-1] <1-9>: "))
          (if (null order) (setq order "1-9"))
        )
        (progn
          (initget "A-Z Z-A")
          (setq order (getkword "\nSort order [A-Z/Z-A] <A-Z>: "))
          (if (null order) (setq order "A-Z"))
        )
      )

      ;; Sort
      (setq sorted
        (vl-sort data
          (function
            (lambda (a b)
              (if numeric
                (if (= order "1-9")
                  (< (atof (nth 1 (nth colIndex a)))
                     (atof (nth 1 (nth colIndex b))))
                  (> (atof (nth 1 (nth colIndex a)))
                     (atof (nth 1 (nth colIndex b))))
                )
                (if (= order "A-Z")
                  (< (strcase (nth 1 (nth colIndex a)))
                     (strcase (nth 1 (nth colIndex b))))
                  (> (strcase (nth 1 (nth colIndex a)))
                     (strcase (nth 1 (nth colIndex b))))
                )
              )
            )
          )
        )
      )

      ;; Write back
      (setq r 1)
      (foreach row sorted
        (setq c 0)
        (foreach cell row
          (setCellData obj r c cell)
          (setq c (1+ c))
        )
        (setq r (1+ r))
      )

      (prompt "\nTable sorted automatically.")
    )
  )

  (princ)
)

  

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

Message 13 of 16

d_salesU4WW6
Contributor
Contributor

You are amazing! Thank you so much! If I'm using a COUNT table will this still update when blocks added?

0 Likes
Message 14 of 16

h_s_walker
Mentor
Mentor
Accepted solution

@d_salesU4WW6 Yes. If you look at the image I posted that was a count table I created, and I added the blocks to it. Just remember that the fields will not be remembered so you will have to keep a spare table somewhere just in case you delete / add blocks, and then you don't have to count them again, just copy the table and run the lisp on the copy.

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 15 of 16

d_salesU4WW6
Contributor
Contributor

Thanks so much - You've been a massive help! Legends have been a massive pain point for us.

0 Likes
Message 16 of 16

h_s_walker
Mentor
Mentor

You're welcome

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes