acad_strlsort will sorts strings, but what if my list looks like
("1" "2" "3" "10" "11" "12" "20")
i am sure i have seen it done ina simple way using vl-sort or vl-sort-i
but am not able to find... can someone help
TIA
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
Covert strings (numbers) to integers then re list them again into strings ..
(setq l '("1" "2" "3" "10" "11" "12" "20")) (foreach x l (setq lst (cons (atoi x) lst))) (setq lst1 (vl-sort lst '<)) (foreach x lst1 (setq lst2 (cons (itoa x) lst2))) (reverse lst2)
Return ..
("1" "2" "3" "10" "11" "12" "20")
Tharwat
@markruys2 wrote:acad_strlsort will sorts strings, but what if my list looks like
("1" "2" "3" "10" "11" "12" "20")
i am sure i have seen it done ina simple way using vl-sort or vl-sort-i
but am not able to find... can someone help
TIA
You can have the conversion to numbers for comparison occur within the sorting function:
Command: (setq test '("21" "4" "35" "2" "1" "14"))
("21" "4" "35" "2" "1" "14")
Command: (vl-sort test '(lambda (a b) (< (atoi a) (atoi b))))
("1" "2" "4" "14" "21" "35")
Or, to use your example list, since it's already sorted one way, maybe you want to sort it in the other direction:
Command: (setq test '("1" "2" "3" "10" "11" "12" "20"))
("1" "2" "3" "10" "11" "12" "20")
Command: (vl-sort test '(lambda (a b) (> (atoi a) (atoi b))))
("20" "12" "11" "10" "3" "2" "1")
If you use DOSLib, you could just use the dos_strsort function:
http://www.en.na.mcneel.com/doslib/string_functions/dos_strsort.htm
Use the "logical" sorting mode.
-- Dale
@Kent1Cooper wrote:....
Command: (vl-sort test '(lambda (a b) (< (atoi a) (atoi b))))
Or, to use your example list, since it's already sorted one way, maybe you want to sort it in the other direction:
....
Command: (vl-sort test '(lambda (a b) (> (atoi a) (atoi b))))
....
Another [and slightly shorter] way to do those same sorts, without the use of (lambda):
Command: (setq test '("21" "4" "35" "2" "1" "14"))
Command: (mapcar 'itoa (vl-sort (mapcar 'atoi test) '<))
("1" "2" "4" "14" "21" "35")
Command: (setq test '("1" "2" "3" "10" "11" "12" "20"))
Command: (mapcar 'itoa (vl-sort (mapcar 'atoi test) '>))
("20" "12" "11" "10" "3" "2" "1")