I want to sort an array based on the values of two columns and a condition on the third. This is my array:
my_array = np.array([[1., 2., 5.1],
[1., 1., 5.],
[2., 2., 2.],
[2., 1., 2.],
[2., 2., 5.],
[3., 2., 2.5],
[3., 1., 2.5],
[2., 1., 5.]])
I must sort it based on the first and second column and also a condition based on the third column. I tried this method:
my_sorted_array = my_array[np.lexsort((my_array[:, 1], my_array[:, 0]))]
but it does not regard my third column. It gives me:
result = np.array([[1. , 1. , 5. ],
[1. , 2. , 5.1 ],
[2., 1. , 2. ],
[2., 1. , 5. ],
[2. , 2. , 2. ],
[2. , 2. , 5. ],
[3. , 1. , 2.5 ],
[3. , 2. , 2.5 ]])
I want to have the following output:
my_sorted_array = np.array([[1., 1., 5.],
[1., 2., 5.1],
[2., 1., 5.],
[2., 2., 5.],
[2., 1., 2.],
[2., 2., 2.],
[3., 1., 2.5],
[3., 2., 2.5]])
I also tried to set coefficients to the third column using this method:
sort_func = my_array[:, 0] * c1 + my_array[:, 1] * c2 + my_array[:, 2] * c3 # c1, c2 and c3 are coefficient
sort_index = np.argsort(sort_func)
This method also is time consuming because I should tune coefficinet for each new data set. Is it possible to put an if_condition in the sorting? How can I rearrange the result into my_sorted_array? Data of two first columns are always gentle and regular (they x and y of a regular grid).
To make it more visual, I uploaded a figure here. The figure shows the trend I want to use to sort my data.

x,yandz) I want to sort them firstly based onxand theny. I have a regular grid (x,y) ofzdata. My problem is that in case some data with lower z values have also low x values and emerge among the values having highzvalues. I will uplad a photo of my real data to show how I like to sort them.z, and sorting in order (z > thresh,x,y). The first item is a boolean mask, and you can usez.meanas the threhsold for now. Is that close enough?