Jump to content

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


Jéferson Gustavo

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

  • 3 months later...
13 hours ago, Grrr said:

Heres something for a generic grid point sort, also Lee covered that 'Ultimate Sorter' challenge of mine, so it seems alot shorter solution.

I found it more complex, I always take time to understand his codes, but I find it very clean and inspiring, nice code!
Thanks!

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