yajis_narif Posted May 13, 2011 Posted May 13, 2011 hiiii!! how I can loop through a list and compare the numbers written on my list to organize them from smallest to largest?? "this is my list ( 25164.4 25164.4 21853.4 21708.6 25678.9 . 12875.3 )" thank youuu Quote
BlackBox Posted May 13, 2011 Posted May 13, 2011 hiiii!! how I can loop through a list and compare the numbers written on my list to organize them from smallest to largest?? "this is my list ( 25164.4 25164.4 21853.4 21708.6 25678.9 . 12875.3 )" thank youuu ^^ Not sure what the stand-alone decimal is doing as part of your list ^^ Here's an example: (vl-load-com) (setq myList (list 25164.4 25164.4 21853.4 21708.6 25678.9 12875.3)) (setq myNewList (vl-sort myList '<)) Example output: (12875.3 21708.6 21853.4 25164.4 25164.4 25678.9) Quote
yajis_narif Posted May 13, 2011 Author Posted May 13, 2011 thank youuu soo much you saved my life :P Quote
BlackBox Posted May 14, 2011 Posted May 14, 2011 thank youuu soo much you saved my life :P You're welcome. Quote
yajis_narif Posted May 14, 2011 Author Posted May 14, 2011 is this applicable on coordinates can i organize them ??? for example i have a list of coordinates like ( (28627.0 8902.0 ) (22425.0 12201.1) (28627.0 15080.1) (25736.0 12201.1) (22425.0 15080.1 ) (22425.0 8902.0 ) (28627.0 15080.1 ) (25316.0 8902.0 ) Quote
Lee Mac Posted May 14, 2011 Posted May 14, 2011 (defun SortByX ( lst ) (vl-sort lst '(lambda ( a b ) (< (car a) (car b)))) ) (defun SortByY ( lst ) (vl-sort lst '(lambda ( a b ) (< (cadr a) (cadr b)))) ) (defun SortByZ ( lst ) (vl-sort lst '(lambda ( a b ) (< (caddr a) (caddr b)))) ) Quote
yajis_narif Posted May 14, 2011 Author Posted May 14, 2011 yes you mean i have to specify (sort by x or y ) cant' i orginize them like the nearest one from the origine of coordinate ??? Quote
pBe Posted May 14, 2011 Posted May 14, 2011 Lee why not... (defun SortByN ( n lst ) (vl-sort lst '(lambda ( a b ) (< (nth n a) (nth n b)))) ) Base 0 index Quote
Lee Mac Posted May 14, 2011 Posted May 14, 2011 yes you mean i have to specify (sort by x or y ) cant' i orginize them like the nearest one from the origine of coordinate ??? You can sort them using any predicate function that returns non-nil for a desired sorting result: (defun SortByMagnitude ( lst ) (vl-sort lst '(lambda ( a b ) (< (distance '(0. 0. 0.) a) (distance '(0. 0. 0.) b)))) ) Of course, the origin could be replaced by any point. Quote
yajis_narif Posted May 14, 2011 Author Posted May 14, 2011 thank youuu so much pBe and lee mac you saved me twice Quote
Lee Mac Posted May 14, 2011 Posted May 14, 2011 thank youuu so much pBe and lee mac you saved me twice But the main thing: do you understand the methodology? Quote
yajis_narif Posted May 14, 2011 Author Posted May 14, 2011 yes i do i just started learning autolisp ... 1 month ago actually can you help me a little bit more when i organize my list (sort by y : ((25736.0 8902.08) (28627.0 8902.08) (22425.0 8902.08) (25316.0 8902.08) (28627.0 11781.1) (25736.0 11781.1) (25316.0 11781.1) (22425.0 11781.1) (25736.0 12201.1) (28627.0 12201.1) (22425.0 12201.1) (25316.0 12201.1) (28627.0 15080.1) (25736.0 15080.1) (25316.0 15080.1) (22425.0 15080.1)) i want to add all the coordinate white the same y in a list so that my list would be like this (((25736.0 8902.08) (28627.0 8902.08) (22425.0 8902.08) (25316.0 8902.08)) ((28627.0 11781.1) (25736.0 11781.1) (25316.0 11781.1) (22425.0 11781.1)) ((25736.0 12201.1) (28627.0 12201.1) (22425.0 12201.1) (25316.0 12201.1)) ((28627.0 15080.1) (25736.0 15080.1) (25316.0 15080.1) (22425.0 15080.1))) thank you Quote
Lee Mac Posted May 14, 2011 Posted May 14, 2011 Not great coding (defun test ( l / sub ) (defun sub ( l ) (if l (cons (cons (car l) (vl-remove-if-not (function (lambda ( x ) (equal (cadar l) (cadr x))) ) (cdr l) ) ) (sub (vl-remove-if (function (lambda ( x ) (equal (cadar l) (cadr x))) ) (cdr l) ) ) ) ) ) (vl-sort (sub l) '(lambda ( a b ) (< (cadar a) (cadar b)))) ) Quote
yajis_narif Posted May 14, 2011 Author Posted May 14, 2011 thank you so much it worked i called the function leeMac (l / sub) lol Quote
Lee Mac Posted May 14, 2011 Posted May 14, 2011 This is a more generic format: (defun test ( l ) (vl-sort (LM:GroupByFoo l (lambda ( a b ) (equal (cadr a) (cadr b))) ) '(lambda ( a b ) (< (cadar a) (cadar b))) ) ) (defun LM:GroupByFoo ( lst foo ) (if lst (cons (cons (car lst) (vl-remove-if-not '(lambda ( x ) (foo (car lst) x)) (cdr lst)) ) (LM:GroupByFoo (vl-remove-if '(lambda ( x ) (foo (car lst) x)) (cdr lst)) foo) ) ) ) _$ (test '((1 2) (3 2) (5 2) (2 4) (3 5) (5 3) (2 3) (6 3) (9 4) (2 4) (7 5) (2 5))) ( ((1 2) (3 2) (5 2)) ((5 3) (2 3) (6 3)) ((2 4) (9 4) (2 4)) ((3 5) (7 5) (2 5)) ) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.