Jump to content

organize a list from smallest to largest number


yajis_narif

Recommended Posts

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

Link to comment
Share on other sites

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) 

Link to comment
Share on other sites

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 )

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ???

Link to comment
Share on other sites

Lee why not...

 

 
(defun SortByN ( n lst )
 (vl-sort lst '(lambda ( a b ) (< (nth n a) (nth n b))))
)

 

Base 0 index

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Not great coding :oops:

 

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

Link to comment
Share on other sites

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))
)

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...