PDA

View Full Version : sort list with extra


parkerfeldman
2009-03-13, 05:48 PM
hello i posted a couple weeks ago about how to sort a list and got awesome responses as to how. i am trying to sort a list that looks like this
(
(("PANEL-MARK" "E-102")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-101")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-104")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102b")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-103")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102a")("Qty" 1) (1917.09 839.649 0.0))
)
i need to sort is by the "E-###" place so the (cadr (assoc "PANEL-MARK")) the issue is that some of the marks will have a character after them like a b c d and i need it to sort the list by this character too if it does have it or atleast group them all together. my list is getting sorted like this..
(
(("PANEL-MARK" "E-101")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102b")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102a")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-103")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-104")("Qty" 1) (1917.09 839.649 0.0))
)
where the E-102's are the issue. any way to fix this to come out in order? 102 102a 102b or at least maybe group the 102's together?

TimSpangler
2009-03-13, 06:23 PM
You may need to use:

chr and ascii along with strcase to sort beyond what you have now.

also take a look at:

(vl-string->list string)

This gives you a list of ascii numbers from the string provided. Check the length of that list then sort the list, group the list by number then check the last element in the list for ascending order and sort that group.

andrea.andreetti
2009-03-13, 08:04 PM
(setq List1 '(
(("PANEL-MARK" "E-102")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-101")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-104")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102b")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-103")("Qty" 1) (1917.09 839.649 0.0))
(("PANEL-MARK" "E-102a")("Qty" 1) (1917.09 839.649 0.0))
))


(defun SORTLIST1 (MyList / List2)
(setq List2 nil)
(foreach n MyList
(setq List2 (append List2 (list (cadar n))))
)
(setq List2 (vl-sort List2 '<))
(setq NewList nil)
(foreach n List2
(foreach i MyList
(if (eq n (cadar i))
(setq NewList (append NewList (list i)))
)
)
)
NewList
)
(SORTLIST1 List1)