Create a new list, iterate through your matrix and add all the values to the list, then call list.Sort()
Something like:
List<int> pixelValues = new List<int>();
pixelValues.Sort();
Best I can come up with using the provided information. You'll want to replace int with whatever type you're storing in your matrix, and I'm not sure what the matrix's structure looks like but I'm guessing you know how to iterate through it.
If you need something other than the default comparison there are overloads of Sort available, see the
MSDN page for List[
^].