Sorting a list of points with y values

I was wondering if there was anyway to sort a list of points using their y values but still being able to access its x values. In my project I want to sort the list L1 based on its y values but then see the x values that go with each sorted point. The image is what I tried to do.

Is it possible to do this? Thanks!

Hello, can you share more about what you need to accomplish? There are some straight forward solutions if, for example, (1) every point has a different y-coordinate, or (2) you don’t need the full sorted list, just the x-coordinate of of the point with the greatest y-coordinate.

Those are just examples of considerations that could simplify what needs to be done. If you have more info about how the list is generated or what your end goal is, we might be able to help more!

In the worst-case scenario (some y-coordinates show up more than once, the full sorted list is needed), here is one solution.

This probably is not the fastest or cleanest solution…but it does work. List experts, chime in with other ideas :slight_smile:

1 Like

Hello! Thank you so much for the reply. Your solution worked fairly well however the problem is that my list has negative numbers as well. Basically I have a line that is the function, f\left(x\right)=\ln\left(2\right)/\left(\ln\left(3\right)-\ln\left(2\right)\right)\cdot x

Then what I did was round this function so that I can find the difference between points on this line and a whole number. This is because I the line is a list of solutions, but they don’t have to be exact, just the close to the whole number. Then I plotted this as points with the restriction that each x value has to be a whole number. The points are shown in the image below

Then I want to sort these points so I can see the ones that are the closest to the x-axis as possible. Your solution works with the sorting, but it just sorts from the lowest number to the highest number. Is there a small change that I could make to make it start from 0? I hope that made some sense!

Thank you

Thanks for the context! I think this should do the trick:

To sort by “closest to 0,” I tweaked the original list by taking the absolute value of the y-coordinates.

1 Like

Thank you so much! It worked wonderfully.