I'm trying to find the line which best fits to the data. I use the following code below but now **I want to have the data placed into an array sorted so it has the data which is closest to the line first how can I do this?** Also is polyfit the correct function to use for this?

```
x=[1,2,2.5,4,5];
y=[1,-1,-.9,-2,1.5];
n=1;
p = polyfit(x,y,n)

f = polyval(p,x);
plot(x,y,'o',x,f,'-')

```

[![Image](https://i.stack.imgur.com/ML3gb.png)](https://i.stack.imgur.com/ML3gb.png)

PS: I'm using Octave 4.0 which is similar to Matlab