# How to sort eigenvectors from lowest to highest corresponding eigenvalue?

**URL:** https://discourse.julialang.org/t/how-to-sort-eigenvectors-from-lowest-to-highest-corresponding-eigenvalue/25586
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [June 23, 2019, 10:10pm UTC](https://discourse.julialang.org/t/how-to-sort-eigenvectors-from-lowest-to-highest-corresponding-eigenvalue/25586 "2019-06-23T22:10:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [June 23, 2019, 10:10pm UTC](https://discourse.julialang.org/t/how-to-sort-eigenvectors-from-lowest-to-highest-corresponding-eigenvalue/25586/1 "2019-06-23T22:10:26Z")

</div>

I’m solving a Sturm-Liouville problem numerically, and in order to use the eigenvectors and eigenvalues I get out of it, it would be handy to be able to order the eigenvalues in order of ascending absolute value, and simultaneously reorder the eigenvectors appropriately so that they match up. So, say we’re dealing with eigenvalues and eigenvectors of H then if:

```julia
EIG=eigen(H);
Y=EIG.vectors;
Lam=EIG.values;

```

then, after the desired reordering, `Lam[1]` would be the eigenvalue of smallest absolute value and `Y[:,1]` would be its corresponding eigenvector.

If I am somehow unclear then I’ll explain what I’d do to accomplish this in MATLAB, I’d run, assuming `[Y, Lam]=eig(H); Lam=diag(Lam);`:

```matlab
[Lam, IX]=sort(Lam, 'ascend');
Y=Y(:,IX);

```

. Is there a way to achieve this in Julia? I know that `Lam=sort(Lam)` would reorder Lam’s eigenvalues in ascending order of value, but it would not also reorder the eigenvectors appropriately.

---

<div class="post-metadata">

### Author: ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)
#### Post date: [June 23, 2019, 10:58pm UTC](https://discourse.julialang.org/t/how-to-sort-eigenvectors-from-lowest-to-highest-corresponding-eigenvalue/25586/2 "2019-06-23T22:58:00Z")

</div>

`sort` and `sortperm` should… sort you out.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 23, 2019, 11:33pm UTC](https://discourse.julialang.org/t/how-to-sort-eigenvectors-from-lowest-to-highest-corresponding-eigenvalue/25586/3 "2019-06-23T23:33:50Z")

</div>

```julia
Y = Y[sortperm(Lam)]

```

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [June 24, 2019, 5:48am UTC](https://discourse.julialang.org/t/how-to-sort-eigenvectors-from-lowest-to-highest-corresponding-eigenvalue/25586/4 "2019-06-24T05:48:50Z")

</div>

Small correction:

```julia
Y = Y[:, sortperm(Lam)]

```
