# Eigen values and vectors sorting

**URL:** https://discourse.julialang.org/t/eigen-values-and-vectors-sorting/98637
**Category:** General Usage
**Tags:** linearalgebra, sortperm, eigenvalues, eigenvectors
**Created:** [May 10, 2023, 6:22pm UTC](https://discourse.julialang.org/t/eigen-values-and-vectors-sorting/98637 "2023-05-10T18:22:51Z")
**Posts on this page:** 1
**Showing post:** 8

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 10, 2023, 11:33pm UTC](https://discourse.julialang.org/t/eigen-values-and-vectors-sorting/98637/8 "2023-05-10T23:33:41Z")

</div>

Note that the `eigen` function takes a parameter `sortby` that is a function specifying how you want them to be sorted. If you want them sorted in descending order by the real part, you can just pass `sortby=-`:

```julia
julia> A = [i^2 + i*j + j^2 for i=1:4, j=1:4]
4×4 Matrix{Int64}:
  3 7 13 21
  7 12 19 28
 13 19 27 37
 21 28 37 48

julia> eigen(A, sortby=-)
Eigen{Float64, Float64, Matrix{Float64}, Vector{Float64}}
values:
4-element Vector{Float64}:
 97.34671140716996
  0.11020703271515231
  3.552713678800501e-15
 -7.456918439885142
vectors:
4×4 Matrix{Float64}:
 -0.260069 -0.629181 0.223607 -0.697492
 -0.374035 0.436083 -0.67082 -0.468966
 -0.526594 0.519101 0.67082 -0.0568572
 -0.717747 -0.380128 -0.223607 0.538835

```

Or if you wanted them in descending order by magnitude, you could pass `sortby = λ -> -abs(λ)`.

This is usually more convenient than re-ordering them afterwards.

---

_[View the full topic](https://discourse.julialang.org/t/eigen-values-and-vectors-sorting/98637)._
