I have a $n$ by $2$ matrix that has binary values(0,1) as its entry. For example
m={{1,Null},{Null,0},{Null,Null},{0,0},{1,0},{0,1}}
I want it to be sorted according to a simple rule: 1) place non-empty ones at the top and empty ones at the bottom; 2) if a row has only one non-empty value, the row that has a non-empty value in column 2 places before the row that has a non-empty value in column 1. For example, {Null,0}
should be placed before {1,Null}
.
So, the final list I want to have should look like:
m_sorted={{0,0},{1,0},{0,1},{Null,0},{1,Null},{Null,Null}}
.
How can I do this type of sorting?
{Null, 0}
should be placed before{1, Null}
" - and yet in your sorted list, you have{1, Null}, {Null, 0}
. Do make up your mind. $\endgroup$Null
is going to slow things down compared to using another integer, say, -1. The reason is that Mathematica can "pack" arrays that only contain one type (typically ints or floats). UsingNull
prevents that. $\endgroup$