# Sorting of a list of lists - i.e. list of matrices - what is the order of the sort determined by?

In the documentation we have:

Sort[list] sorts the elements of list into canonical order.

Maybe I am missing something but what does "canonical order" mean for sorting a list of matrices?

What is matrix "canonical order"?

NOTE - I am talking about a list of matrices where ALL the matrices are of the same dimension - i.e. all n x n

• Go to the documentation and click on "Details". For example it says that "Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth-first manner". I think this means that a $5 \times 100$ matrix comes before a $7 \times 3$ matrix because the $5 \times 100$ is shorter in the sense of Length. Does that answer your question? Jul 7, 2022 at 10:41
• @user293787 My matrices are all the same size (actually all square as well). I don't see anything about what determines canonical order for matrices of the same dimensions. Jul 7, 2022 at 10:43
• See "...and then comparing parts in a depth-first manner". I think (?) this means that it will first compare first rows. If there is again a tie, it will compare second rows. Try Sort[{{{1,2},{0,0}},{{0,50},{99,100}}}] for example, the order remains the same regardless of what you do with the second rows. Jul 7, 2022 at 10:45
• @user293787 Hmmm, the documentation is rather vague with that wording. I also don't think it is actually doing that. When I have a chance I'll amend things with an example which I think shows it cannot be doing what you describe. Jul 7, 2022 at 10:50
• This question could be considered a duplicate, see here. Jul 7, 2022 at 12:21

Here's the relevant quote from the documentation: "Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth‐first manner."

So for matrices, it would order them by their size, i.e. 2x2-matrices first, 3x3-matrices second, ... .

Matrices of the same size would be ordered by the first element, i.e. the [1,1] entry. Here is a short example for 2x2 matrices:

m1 = {{1, 6}, {7, 8}};
m2 = {{5, 2}, {3, 4}};
Sort[{m2, m1}]


returns

{{{1, 6}, {7, 8}}, {{5, 2}, {3, 4}}}


so {m1,m2}.

You might consider ordering the matrices by some property like the determinant or trace to give more meaning to the ordering.

• My matrices, as I have amended in the question, are all the same dimension. My question is what is the "canonical order" that Sort uses for a list of matrices. I just saw your statement in regard to the [1,1] entry - well it sure isn't doing that from what I see. I'll get back to this when I can. Jul 7, 2022 at 10:51
• Sorry, I just saw the edit after my first answer. :)
– Flow
Jul 7, 2022 at 10:57
• Since each entry of your m1 is smaller than each entry of m2, it does not really support your point that the $(1,1)$ entry is decisive. Why not use m1={{1,6},{7,8}}; m2={{5,2},{3,4}}; instead? Jul 7, 2022 at 10:59
• That still holds, I updated the example to your suggestion.
– Flow
Jul 7, 2022 at 11:01
• I think about it like this: nxn matrices are of same length, so the first sentence of the documentation quote doesn't help. What's the next depth level below that? The first rows, which are again of same length n. The next level is then the first entry, let's assume a scalar number. And this will be sorted by magnitude. Thus the same size matrices are sorted by their first entry.
– Flow
Jul 7, 2022 at 11:02

The default Sort criteria for any array is lexicographical order, putting small number above and large ones behind and if facing the same elements it will compare the next dimension/array depth. However you can define a customized function for matrix or any type of array. For example sort by matrix norm:

a1 = RandomReal[1, {2, 2}]; a2 = RandomReal[1, {2, 2}]; a3 =
RandomReal[1, {2, 2}];
{b1,b2,b3}=Sort[{a1, a2, a3}, Norm[#] &];


This will give you correct order.