# How to sort eigenvalues (from minimum to maximum) and their corresponding eigenvectors?

When using NDEigensystem, the first eigenvalue corresponds to the first eigenvector, the second eigenvalue to the second eigenvectors, and so on. I would like to sort the eigenvalues from min to max, also so that the eigenvectors are also sorted accordingly. How can this be implemented?

in the code, I do not provide eigenvectors, because they are displayed as pictures

ClearAll["Global*"]

{vals, funs} =
NDEigensystem[-Laplacian[u[x], {x}] + (-(1/x) + x^2)*u[x],
u[x], {x, 0, \[Pi]}, 10]

(*{{2.89816, 6.93897, -10.8147, 10.9162, 17.2964, 26.0667,
36.977, 49.9732, 65.0544,
82.2449}, {InterpolatingFunction[{{0., 3.141592653589793}}, {
5, 4225, 0, {41}, {3}, 0, 0, 0, 0, Indeterminate& , {}, {},
False},...*)


You can see that Mathematica outputs the eigenvalues in an arbitrary order (the eigenvectors are arranged in the same order). Now I would like to sort the eigenvalues from minimum to maximum, but in such a way that their corresponding eigenvectors are sorted in the appropriate order.

If it's simply applied Sort, then the eigenvalues are sorted from minimum to maximum, but the eigenvectors are not sorted accordingly.

Sort /@
NDEigensystem[-Laplacian[u[x], {x}] + (-1/x + x^2)*u[x],
u[x], {x, 0, \[Pi]}, 10]

(*{{-10.8147, 2.89816, 6.93897, 10.9162, 17.2964, 26.0667,
36.977, 49.9732, 65.0544,
82.2449}, {InterpolatingFunction[{{0., 3.141592653589793}}, {
5, 4225, 0, {41}, {3}, 0, 0, 0, 0, Indeterminate& , {}, {},
False},...*)

• Reverse /@ NDEigensystem[-Laplacian[u[x], {x}] + (1/x + x^2)*u[x], u[x], {x, 0, \[Pi]}, 10]? Commented Jun 5, 2023 at 5:32
• @Henrik Schumacher, thanks! I edited the question, please take a look Commented Jun 5, 2023 at 7:16
• Try: ord = Ordering[vals]; {vals,funs}={vals[[ord]], funs[[ord]]} Commented Jun 5, 2023 at 7:22
• @Daniel Huber, thanks! Commented Jun 5, 2023 at 10:25
• You could also look at SortBy Commented Jun 5, 2023 at 13:11

{vals, funs} // Transpose // Sort // Transpose
`