This question requires idea of the Linear Combination of Atomic Orbitals (LCAO). It involves the following equation:
$\psi_n = \sum\limits_{i} c_{ni}\phi_{i} = c_{n1}\phi_{1} + c_{n2}\phi_{2} + ... c_{ni}\phi_{i}$
Here $\psi_n$ represents the wavefunction for the resulting molecular orbital, $\phi_{i}$ represents the atomic orbitals that contribute to the bond, and $c_{ni}$ represents the weight coefficient that can basically tell us how much of each atomic orbital is involved in the molecular orbital. If we look at the molecular orbital diagram for carbon monoxide, you are right, the bonding pi orbitals are lower in energy than the bonding sigma orbital:
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/xFqjn.png)
The reason for this is due to the contributions from carbon. This comes from the relative energies of the atomic orbitals. From photoelectron spectroscopy, the energies of the carbon 2p orbitals and 2s can interact with the oxygen 2p orbitals because they are close enough in energy while the 2s oxygen orbital is a little too far to interact with the 2p orbitals of carbon. See image below
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/SQBKT.png)
The important thing to note here is that the 2s orbital can interact as well. This means when we solve for the wave function using LCAO and the equation above, carbon can give a large contribution because two of its atomic orbitals interact, and it turned out it did. The same thing can be done for NO (which in fact is also similar to CO where the pi bonding orders are lower energy than the sigma bonding ones) and other molecules. However, it gets extremely complicated with larger and larger molecules.
I got this information from this public PowerPoint https://www.chem.uci.edu/~lawm/10-9.pdf which come from lectures from UCIrvine which were released: https://www.youtube.com/watch?v=1CH6CeORL_g&list=PLaLOVNqqD-2GDVq2xFUuAltjIXPgQNNTL&index=7&ab_channel=UCIOpen