Jump to content
Jéferson Gustavo

Function to sort a list of points, first x then y

Recommended Posts

Jéferson Gustavo

Hello everyone, I need your help to create a function, which orders a list of points first by the first element (x), then by the second (y). I need this function to select objects from left to right, top to bottom.
An example to be clearer:
a list of points ((1 2 0) (1 1 0) (2 3 0) (2 1 0) (1 3 0) (2 2 0))
When applying the function returns: ((1 1 0) (1 2 0) (1 3 0) (2 1 0) (2 2 0) (2 3 0)).
Thank you in advance!
Note: forgive my English, I'm using a translator.

Share this post


Link to post
Share on other sites
dlanorh
9 hours ago, Jéferson Gustavo said:

Hello everyone, I need your help to create a function, which orders a list of points first by the first element (x), then by the second (y). I need this function to select objects from left to right, top to bottom.
An example to be clearer:
a list of points ((1 2 0) (1 1 0) (2 3 0) (2 1 0) (1 3 0) (2 2 0))
When applying the function returns: ((1 1 0) (1 2 0) (1 3 0) (2 1 0) (2 2 0) (2 3 0)).
Thank you in advance!
Note: forgive my English, I'm using a translator.

 

Try

 

(setq lst (vl-sort lst '(lambda (x y) (if (= (car x) (car y)) (< (cadr x) (cadr y)) (< (car x) (car y))))))

where lst is the list you wish to sort

 

This returns as per your post, however this is not left->right top->bottom but left->right bottom->top

 

If you want top->bottom

 

(setq s_lst (vl-sort lst '(lambda (x y) (if (= (car x) (car y)) (> (cadr x) (cadr y)) (< (car x) (car y))))))

 

should work

Edited by dlanorh
  • Thanks 1

Share this post


Link to post
Share on other sites
Jéferson Gustavo
4 hours ago, dlanorh said:

 

Try

 


(setq lst (vl-sort lst '(lambda (x y) (if (= (car x) (car y)) (< (cadr x) (cadr y)) (< (car x) (car y))))))

where lst is the list you wish to sort

 

This returns as per your post, however this is not left->right top->bottom but left->right bottom->top

 

If you want top->bottom

 


(setq s_lst (vl-sort lst '(lambda (x y) (if (= (car x) (car y)) (> (cadr x) (cadr y)) (< (car x) (car y))))))

 

should work

Tested it and it worked perfectly. Thank you very much!

Share this post


Link to post
Share on other sites
BIGAL

You can go deeper in a sort than X Y

 

eg block-name X Y Z att1 att2

Edited by BIGAL
  • Thanks 1

Share this post


Link to post
Share on other sites
Jéferson Gustavo
11 hours ago, BIGAL said:

You can go deeper in a sort than X Y

 

eg block-name X Y Z att1 att2

good to know, thanks!

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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