lisp sort list via function

function, lisp, list, sorting

Solution

Use `:key` with `sort`:

(sort list #'< :key (lambda (p) (dist p start-point)))

This will sort the `list` of points in the increasing order (use `>` for decreasing) based on the distance to `start-point`.

Problem

I am trying to use lisp's sort to sort a list via a function but dont have a clue how to do this. I have a start-point in 2D Space with x and y coordinates. Then i have a List of N-other points and i have a function that calculates the distance between 2 points. What I want now is a list, that contains all the N-Points and is sorted by distance ascending from the start-point to all other points. I think I can use the sort-function and pass a function as argument (the calculate-distance function) But i dont know how to do it and researches on the web did not help. Any ideas? Regards

Original source