@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

(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.

Left Handed and Proud