Added method to write csv in a sorted matrix format.