# Double Sorting a Matrix

Say I have two matrices

{{0, 1, 2, 2},
{2, 1, 0, 2}}


and

{{2, 1, 0, 2},
{2, 1, 2, 0}}


I then sort by the first row to get {{0, 1, 2, 2}, {2, 1, 0, 2}} and {{0, 1, 2, 2}, {2, 1, 2, 0}}.

Now I would like to further sort the elements of the lower row which correspond to the same value in the upper row. In this particular case, the last two columns of the upper row have the same value, 2, thus I want to sort those last two columns in the lower row.

If done correctly, we would expect both of the matrices above to reduce to

{{0, 1, 2, 2},
{2, 1, 0, 2}}


What I think you are asking for can be achieved by 1) pairing up the values in the two rows (using Transpose) 2) Sorting by the First value in each pair using SortBy 3) Unpairing, again using Transpose. Specifically
lists = {{2, 1, 0, 2}, {2, 1, 2, 0}}
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.38594186305999756, "perplexity": 331.76948200509975}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-43/segments/1634323585302.56/warc/CC-MAIN-20211020024111-20211020054111-00483.warc.gz"}