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

## 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)).
Note: forgive my English, I'm using a translator.

##### 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)).
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
• 1

##### 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!

##### Share on other sites You can go deeper in a sort than X Y

eg block-name X Y Z att1 att2

Edited by BIGAL
• 1

##### Share on other sites 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!

## 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. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.