Sort array of vectors by first element of each vector

Say I have an array of 4 vectors, each vector being 5-dimensional. The current ordered declaration

ordered[5] a[4];

would take each of the 4 vectors and order the elements in each vector. Instead of this, I want to compare the 4 vectors by their first elements, then re-arrange the orders of the vectors - in this example, it would be re-arranging the rows of the array of vectors by the elements in the first column. Is there a good way to do this in Stan?


there is no straightforward way of doing this with a single function. The only way of doing this that I can see is creating a user-defined function that takes in an array of vector and creates a new array of vectors by doing a custom sort: find the smallest first element and place that as the first vector in the array of vectors, then find the second smallest first element, etc.