Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a matrix A, we can say that it is "sorted" if each row and column of A is sorted (ascending left-to-right for rows and top-to-bottom for columns). Thus, this matrix is sorted:

[ 1 2 3 4 5 ]
[ 2 2 3 4 5 ]
[ 3 3 3 4 5 ]
[ 4 4 4 4 5 ]
[ 5 5 5 5 5 ]

However, this matrix is not sorted:

[ 1 2 3 4 5 ]
[ 2 1 3 4 5 ]
[ 3 3 3 4 5 ]
[ 4 5 4 5 5 ]
[ 5 5 5 5 5 ]

The second column (transpose of [2, 1, 3, 5, 5]) and fourth row ([4, 5, 4, 5, 5]) are not sorted in ascending order.

Given a matrix (either as a 2D rectangular nested list or a string with consistent, distinct delimiters between rows and between values) and optionally the width and/or height of the matrix, output a sorted matrix (in the same format) with the same dimensions and containing the same values as the input matrix.

The values in the matrix will always be in the inclusive range [-128, 127] (8-bit signed two's complement integers). There is no limit on the dimensions of the input matrix.

Examples

Because there are multiple valid outputs for each input, these are possible outputs. Any output that fulfills the conditions is valid. The examples are given with Python list notation.

[[0, -29]] -> [[-29, 0]]
[[-36, -18, 0], [8, 99, 112], [14, 6, -12]] -> [[-36, -18, 0], [-12, 6, 14], [8, 99, 112]]
[[-20, -102, -41, -57, -73, 58], [49, 82, 60, -53, 58, 60], [-32, -127, -23, -70, -46, -108], [-56, -15, 98, -90, 73, -67], [97, 74, 59, 126, -7, 46], [-101, 42, -39, -52, 89, 29]] -> [[-127, -108, -101, -70, -53, -32], [-102, -90, -67, -52, -23, 42], [-73, -57, -46, -20, 46, 59], [-56, -41, -15, 49, 60, 74], [-39, -7, 58, 60, 82, 97], [29, 58, 73, 89, 98, 126]]
[[23, 82, 94, 7, -39, 70, -31, -120], [-13, 78, -70, 28, -10, 30, 101, 48], [60, -111, 32, 93, 91, 77, -27, 7], [-37, -41, -8, 80, 102, 18, -16, -48], [97, -14, -102, -53, 108, -92, -83, 108], [-67, -121, -15, -9, 91, -89, -127, -109], [-127, -103, -48, -2, -106, 3, -114, 97], [28, -78, -40, 52, 39, 115, -88, 10]] -> [[-127, -127, -120, -109, -92, -70, -40, -14], [-121, -114, -106, -89, -67, -39, -13, 7], [-111, -103, -88, -53, -37, -10, 10, 32], [-102, -83, -48, -31, -9, 18, 39, 77], [-78, -48, -27, -8, 23, 48, 78, 91], [-41, -16, -2, 28, 52, 80, 93, 97], [-15, 3, 28, 60, 82, 94, 101, 108], [7, 30, 70, 91, 97, 102, 108, 115]]
[[85, 90, -65, -38, -58, -71, 123, -83, 44], [55, -34, 21, 103, -10, 59, -109, 30, -41], [108, -106, -90, 74, 117, 15, -63, 94, -37], [43, -118, -126, -45, 77, -62, 22, 76, 9], [56, 31, 58, 51, -64, 125, -48, -123, -108], [77, -53, -61, 99, -16, -21, -98, -50, 60], [127, -113, -9, 33, -6, -102, -47, -122, 31], [-59, -23, 0, 21, 14, 61, 83, 47, -6], [97, -120, -111, 113, -68, -128, -81, 68, 88]] -> [[-128, -126, -122, -113, -106, -81, -62, -47, -16], [-123, -120, -111, -102, -71, -61, -45, -10, 21], [-118, -109, -98, -68, -59, -41, -9, 21, 44], [-108, -90, -65, -58, -38, -6, 22, 47, 60], [-83, -64, -53, -37, -6, 30, 51, 61, 77], [-63, -50, -34, 0, 31, 55, 68, 83, 94], [-48, -23, 9, 31, 56, 74, 85, 97, 108], [-21, 14, 33, 58, 76, 88, 99, 113, 123], [15, 43, 59, 77, 90, 103, 117, 125, 127]]
[[-80, -42, 1, 126, -42, -34, 81, 73, 6, 9, 72], [-98, -98, -82, 63, -16, -116, 29, 61, 119, 20, 19], [-99, -70, 41, 44, 117, 61, 89, -117, 92, -3, -49], [18, 122, 126, 84, 79, 114, -61, 45, 80, 109, -107], [-77, 73, -62, -58, -25, -24, 126, -14, -13, -90, -60], [84, 1, 95, 21, -13, 26, 75, 78, -36, -76, -91], [54, -58, 79, -128, 63, 119, 79, 106, 103, 125, 98], [126, 37, -77, -15, -57, 63, 22, -98, 93, 67, 41], [93, -45, -91, 34, 29, -38, -103, 109, -92, 6, -115], [32, -112, -29, -65, 61, 20, 80, -41, -68, 28, 25], [56, -112, 47, -88, 56, -35, -26, 13, 122, 27, -70], [108, -69, -42, 24, 94, -20, -46, 90, -98, 112, 32]] -> [[-128, -117, -115, -107, -98, -91, -77, -62, -45, -29, -3], [-116, -112, -103, -98, -90, -76, -61, -42, -26, 1, 21], [-112, -99, -98, -88, -70, -60, -42, -25, 1, 22, 34], [-98, -92, -82, -70, -58, -42, -24, 6, 24, 37, 61], [-91, -80, -69, -58, -41, -20, 6, 25, 41, 61, 73], [-77, -68, -57, -38, -16, 9, 26, 41, 61, 75, 81], [-65, -49, -36, -15, 13, 27, 44, 63, 78, 84, 93], [-46, -35, -14, 18, 28, 45, 63, 79, 84, 94, 108], [-34, -13, 19, 29, 47, 63, 79, 89, 95, 109, 117], [-13, 20, 29, 54, 67, 79, 90, 98, 109, 119, 122], [20, 32, 56, 72, 80, 92, 103, 112, 119, 125, 126], [32, 56, 73, 80, 93, 106, 114, 122, 126, 126, 126]]
[[53, 109, -41, 66, 63, -108, -24, 85, 28, 57, -11, -94, -16], [-28, -113, 58, 115, -28, -124, 71, -109, -65, 45, 75, 97, 107], [124, 23, 101, 112, -64, 19, 21, 34, 6, -2, -70, -92, 122], [19, 94, 80, -105, -3, -125, -2, 44, -24, 41, -30, 64, 32], [36, -44, 59, -28, -36, -11, 111, -64, 78, 120, 1, 102, 49], [-128, 67, 17, -9, -64, -86, 117, 7, 118, 7, -11, -82, 124], [5, -36, 22, 98, -78, -33, 100, 92, -55, 125, -28, 24, -6], [97, 31, -106, -15, 8, 80, -86, -107, -105, -5, -71, 76, 124], [-83, 24, -116, 66, 82, -32, -19, 111, -84, -77, -14, 67, -70], [77, -111, -101, -91, -23, 36, 24, -33, 13, -90, -9, 32, -54], [51, -31, 125, -25, -61, 5, 71, -81, -3, -39, 109, -17, -97], [61, -46, -122, 76, 13, -101, 24, 97, 39, -29, -22, -3, -116], [56, 0, -4, 71, -116, 115, 79, -83, 74, 44, -77, 42, -30], [-72, 45, -109, -82, 43, 38, -117, 1, 69, -66, -18, 108, 8]] -> [[-128, -125, -122, -116, -109, -105, -92, -83, -71, -55, -32, -24, -9], [-124, -117, -116, -109, -105, -91, -82, -70, -54, -31, -23, -9, 5], [-116, -113, -108, -101, -90, -82, -70, -46, -30, -22, -6, 5, 22], [-111, -107, -101, -86, -81, -66, -44, -30, -19, -5, 6, 23, 36], [-106, -97, -86, -78, -65, -41, -29, -18, -4, 7, 24, 38, 51], [-94, -84, -77, -64, -39, -28, -17, -3, 7, 24, 39, 53, 66], [-83, -77, -64, -36, -28, -16, -3, 8, 24, 41, 56, 67, 76], [-72, -64, -36, -28, -15, -3, 8, 24, 42, 57, 67, 76, 85], [-61, -33, -28, -14, -2, 13, 28, 43, 58, 69, 77, 92, 100], [-33, -25, -11, -2, 13, 31, 44, 59, 71, 78, 94, 101, 109], [-24, -11, 0, 17, 32, 44, 61, 71, 79, 97, 102, 111, 115], [-11, 1, 19, 32, 45, 63, 71, 80, 97, 107, 111, 117, 122], [1, 19, 34, 45, 64, 74, 80, 97, 108, 112, 118, 124, 124], [21, 36, 49, 66, 75, 82, 98, 109, 115, 120, 124, 125, 125]]
[[47, -58, 7, -88, 126, -87, 103, 125, 83, 32, 116, 107, -92, -96, 110, -102], [-75, -81, 53, -93, 91, -5, -4, 104, 88, -73, -114, -113, 126, 78, -114, -3], [125, -68, -88, -17, 1, 53, -124, -59, -19, 87, -60, 55, -30, -6, 39, 37], [-38, -123, 125, 119, -43, 11, -25, -89, -57, 112, 123, 9, -76, -125, 118, 68], [-119, -97, -42, 73, 80, 108, -96, -54, -110, 115, -58, -67, -9, 94, 71, -56], [-25, 109, -51, 71, 61, 12, 122, -99, -16, -87, -127, -76, 46, 102, 52, 114], [97, 26, -112, 49, -44, -26, -93, -119, 21, 101, 83, -112, 14, 41, 120, 37], [90, 95, 89, 73, 51, -33, 3, -125, -106, -83, -5, -26, 33, -119, -74, 41], [9, -81, 116, -124, -8, -15, 65, 104, 41, -92, 2, 51, 33, 115, -47, 30], [87, -127, 121, 42, 33, -22, 28, -74, 26, 55, 126, -70, 0, -63, -40, -51], [-117, 79, -113, -4, 78, -33, -54, -40, -1, -48, 60, 91, 119, 117, -75, 114], [7, 102, 6, 77, -112, -128, 34, 112, -82, -17, -120, -96, -101, -79, -27, -84], [-74, -77, 67, -78, -72, 80, 59, 115, -76, -7, 66, -28, 120, 117, 56, -46], [80, 42, -121, -5, 73, -82, -115, -72, 10, -120, -26, -82, -22, 110, -7, -119], [10, -88, 39, 92, -16, 58, -40, 79, 116, 75, 96, -102, 4, 93, 46, -95], [20, -61, 110, 18, -103, -87, -67, -26, -74, -22, 1, -106, -81, -20, 10, 87]] -> [[-128, -127, -125, -124, -120, -117, -112, -102, -95, -87, -81, -74, -59, -43, -26, -7], [-127, -125, -123, -119, -115, -112, -102, -93, -87, -79, -74, -58, -42, -25, -7, 6], [-124, -121, -119, -114, -112, -101, -93, -87, -78, -73, -58, -40, -25, -6, 7, 26], [-120, -119, -114, -110, -99, -92, -84, -77, -72, -57, -40, -22, -5, 7, 28, 41], [-119, -113, -106, -97, -92, -83, -76, -72, -56, -40, -22, -5, 9, 30, 42, 55], [-113, -106, -96, -89, -82, -76, -70, -54, -38, -22, -5, 9, 32, 42, 56, 73], [-103, -96, -88, -82, -76, -68, -54, -33, -20, -4, 10, 33, 46, 58, 73, 80], [-96, -88, -82, -75, -67, -51, -33, -19, -4, 10, 33, 46, 59, 73, 83, 91], [-88, -81, -75, -67, -51, -30, -17, -3, 10, 33, 47, 60, 75, 83, 92, 102], [-81, -74, -63, -48, -28, -17, -1, 11, 34, 49, 61, 77, 87, 93, 103, 110], [-74, -61, -47, -27, -16, 0, 12, 37, 51, 65, 78, 87, 94, 104, 110, 115], [-60, -46, -26, -16, 1, 14, 37, 51, 66, 78, 87, 95, 104, 112, 115, 117], [-44, -26, -15, 1, 18, 39, 52, 67, 79, 88, 96, 107, 112, 116, 118, 120], [-26, -9, 2, 20, 39, 53, 68, 79, 89, 97, 108, 114, 116, 119, 121, 125], [-8, 3, 21, 41, 53, 71, 80, 90, 101, 109, 114, 116, 119, 122, 125, 126], [4, 26, 41, 55, 71, 80, 91, 102, 110, 115, 117, 120, 123, 125, 126, 126]]
[[-88, -62, -59, -18, 118, -13, -93, 75, 44, 67, -122, -1, 117, -121, 118, 13, -33, 44], [-4, -75, 95, 25, 9, -104, 6, 79, -110, 3, -108, 117, 96, 113, 69, 55, 75, -95], [-69, 11, 87, -78, -18, -17, -52, 6, 88, 31, 39, 45, 61, -75, -83, 117, 85, -3], [-27, 83, -86, -69, -29, -15, 62, -90, -127, 53, -71, 77, -95, -86, -20, 69, 103, -111], [3, -6, -70, -121, -58, -72, 88, 105, 68, -31, 86, -28, 69, 78, 13, 88, 19, 75], [69, 73, 116, -2, -93, 15, 74, 58, 98, -100, -54, 95, 47, -126, -71, 63, 84, 113], [110, -42, -33, -87, 109, 86, -75, 25, 83, -25, -76, 84, -42, -57, -93, -9, -90, 3], [-100, 36, -83, 10, -85, 88, -15, 107, -76, -37, 109, 79, -120, 118, -60, 113, -124, -15], [123, 122, -94, 14, -16, 118, -57, -111, 80, 62, 56, 66, 27, -44, -53, -13, 94, -28], [116, -67, 8, -70, -54, -1, 53, 40, -78, 15, -121, -30, -125, -16, -74, 119, 97, 43], [-24, 109, -72, 16, 55, -51, -87, 46, -62, 69, -106, -49, -112, 71, -55, 104, -110, 62], [67, 13, -75, 106, -35, -54, 15, -104, 34, 93, 39, -126, -29, 61, 29, 4, 70, -28], [27, -89, -15, -32, -82, -72, 53, -22, -23, 49, -16, 76, -25, 31, 115, -88, -57, -97], [1, 29, 54, 88, 86, 77, -58, 100, -125, 117, 102, 41, 99, 115, 24, -16, -99, -116], [-85, -47, -108, 26, 18, -107, -88, 110, 27, -118, 88, -122, -85, -94, -33, 51, 40, 77], [-3, 52, -20, 12, 117, 101, 34, -8, -100, -23, 45, 83, -88, -90, -47, 70, 29, -111], [26, -68, 7, 38, -118, -53, -79, -48, 41, -88, 35, 86, 66, 24, 37, 72, -66, -77]] -> [[-127, -126, -125, -122, -121, -112, -108, -100, -93, -88, -83, -75, -68, -54, -37, -25, -15, 4], [-126, -125, -122, -120, -111, -108, -100, -93, -88, -83, -75, -67, -54, -35, -24, -15, 6, 18], [-124, -121, -118, -111, -107, -99, -93, -88, -82, -74, -66, -54, -33, -23, -13, 6, 19, 34], [-121, -118, -111, -106, -97, -90, -87, -79, -72, -62, -53, -33, -23, -13, 7, 24, 34, 45], [-116, -110, -104, -95, -90, -87, -78, -72, -62, -53, -33, -22, -9, 8, 24, 35, 45, 58], [-110, -104, -95, -90, -86, -78, -72, -60, -52, -32, -20, -8, 9, 25, 36, 46, 61, 69], [-100, -94, -89, -86, -77, -71, -59, -51, -31, -20, -6, 10, 25, 37, 47, 61, 69, 75], [-94, -88, -85, -76, -71, -58, -49, -30, -18, -4, 11, 26, 38, 49, 62, 69, 76, 83], [-88, -85, -76, -70, -58, -48, -29, -18, -3, 12, 26, 39, 51, 62, 69, 77, 84, 88], [-85, -75, -70, -57, -47, -29, -17, -3, 13, 27, 39, 52, 62, 70, 77, 84, 88, 96], [-75, -69, -57, -47, -28, -16, -2, 13, 27, 40, 53, 63, 70, 77, 85, 88, 97, 104], [-69, -57, -44, -28, -16, -1, 13, 27, 40, 53, 66, 71, 78, 86, 88, 98, 105, 110], [-55, -42, -28, -16, -1, 14, 29, 41, 53, 66, 72, 79, 86, 88, 99, 106, 110, 115], [-42, -27, -16, 1, 15, 29, 41, 54, 67, 73, 79, 86, 93, 100, 107, 113, 116, 117], [-25, -15, 3, 15, 29, 43, 55, 67, 74, 80, 86, 94, 101, 109, 113, 116, 117, 118], [-15, 3, 15, 31, 44, 55, 68, 75, 83, 87, 95, 102, 109, 113, 117, 117, 118, 119], [3, 16, 31, 44, 56, 69, 75, 83, 88, 95, 103, 109, 115, 117, 118, 118, 122, 123]]
[[84, 18, -122, 74, -47, 17, -69, 121, -79, 110, 10, 122, 84, 19, 77, -57, 25, 87, -42], [95, 89, 10, -1, -24, -93, -26, -39, 11, -15, 47, 23, 114, 36, 121, -87, 106, 120, -86], [48, 66, 65, 28, 74, -22, -67, 77, -77, 19, 88, -24, -88, 85, -34, 13, 103, -102, 86], [108, -17, -122, -13, 63, 61, -56, 24, -48, -3, -85, -57, 11, -52, -26, -24, 48, 100, 18], [-91, -126, 124, 5, -118, 93, 94, -100, -24, 15, 77, -43, 64, 51, 64, 7, -22, -47, 79], [98, 80, 117, -19, -55, -95, -35, -48, -56, -122, -120, 52, 54, 37, -101, -38, -35, 101, -6], [72, 68, 26, -79, -1, 25, -3, -40, 2, 56, 119, 17, -95, 83, -94, -79, -88, -110, 85], [55, 39, 75, 127, 110, 0, 56, -1, 39, 116, 44, 120, -113, 81, 113, 10, 78, 114, -79], [103, 121, 78, -121, -17, 33, 117, 110, -26, 2, -79, 27, -117, 62, -27, -17, -20, 104, 115], [-11, 67, 76, 62, -14, 78, -94, -8, -71, 15, 77, 98, 127, 109, 61, 33, -51, 65, -103], [-91, 97, 83, -61, 22, -31, 20, -119, 40, -48, -3, 34, -70, 23, -80, -73, 5, 23, -102], [109, 78, 124, 118, -39, -3, -114, -50, 0, 79, -68, -34, -96, 104, -120, 41, 2, 108, -17], [-90, -30, -25, -29, -52, -37, -49, 20, 91, -48, -91, 80, -117, -6, -88, -68, 69, 103, 118], [-79, -118, -122, -112, 71, -4, 28, 78, -77, -33, 10, 21, -125, 69, -88, 18, 99, 11, -127], [-124, -53, -4, 80, -94, -44, -124, 94, 97, -55, -89, -78, -37, -38, -40, -11, -116, 84, 18], [44, 32, -44, 76, -101, 85, -67, 69, -4, 20, -89, -103, 117, 18, -121, 84, 18, -91, -106], [107, 58, 6, -72, 112, 96, 39, 77, -4, 104, 60, 112, 39, -102, -4, -11, 80, 36, -117], [96, -79, 119, -65, 80, -35, -60, 4, -63, 92, 76, -46, -2, 59, -86, -105, -76, 106, -102], [-88, -89, 69, -5, 63, 75, -59, -93, 101, 33, 64, 114, -126, -106, 33, 113, 50, 71, 82], [-94, 9, -3, 11, 115, -121, 111, -34, -11, 96, -34, 51, -44, -56, -20, -14, 1, 49, 100]] -> [[-127, -126, -125, -122, -121, -119, -116, -105, -101, -93, -88, -79, -71, -57, -48, -38, -26, -14, -3], [-126, -124, -122, -121, -118, -114, -103, -100, -93, -88, -79, -70, -56, -48, -37, -26, -14, -3, 10], [-124, -122, -121, -118, -113, -103, -96, -91, -88, -79, -69, -56, -47, -37, -25, -13, -3, 10, 19], [-122, -120, -117, -112, -102, -95, -91, -88, -79, -68, -56, -47, -35, -24, -11, -2, 10, 19, 33], [-120, -117, -110, -102, -95, -91, -88, -79, -68, -55, -46, -35, -24, -11, -1, 10, 20, 33, 48], [-117, -106, -102, -94, -91, -87, -79, -67, -55, -44, -35, -24, -11, -1, 11, 20, 33, 48, 62], [-106, -102, -94, -90, -86, -78, -67, -53, -44, -34, -24, -11, -1, 11, 20, 33, 49, 62, 69], [-101, -94, -89, -86, -77, -65, -52, -44, -34, -22, -8, 0, 11, 21, 34, 50, 63, 71, 77], [-94, -89, -85, -77, -63, -52, -43, -34, -22, -6, 0, 11, 22, 36, 51, 63, 71, 77, 80], [-89, -80, -76, -61, -51, -42, -34, -20, -6, 1, 13, 23, 36, 51, 64, 72, 78, 81, 86], [-79, -73, -60, -50, -40, -33, -20, -5, 2, 15, 23, 37, 52, 64, 74, 78, 82, 87, 96], [-72, -59, -49, -40, -31, -19, -4, 2, 15, 23, 39, 54, 64, 74, 78, 83, 88, 96, 101], [-57, -48, -39, -30, -17, -4, 2, 17, 24, 39, 55, 65, 75, 78, 83, 89, 97, 103, 107], [-48, -39, -29, -17, -4, 4, 17, 25, 39, 56, 65, 75, 78, 84, 91, 97, 103, 108, 111], [-38, -27, -17, -4, 5, 18, 25, 39, 56, 66, 76, 79, 84, 92, 98, 103, 108, 112, 114], [-26, -17, -4, 5, 18, 26, 40, 58, 67, 76, 79, 84, 93, 98, 104, 109, 112, 115, 117], [-15, -3, 6, 18, 27, 41, 59, 68, 76, 80, 84, 94, 99, 104, 109, 113, 115, 118, 120], [-3, 7, 18, 28, 44, 60, 69, 77, 80, 85, 94, 100, 104, 110, 113, 116, 118, 120, 121], [9, 18, 28, 44, 61, 69, 77, 80, 85, 95, 100, 106, 110, 114, 117, 119, 121, 122, 124], [18, 32, 47, 61, 69, 77, 80, 85, 96, 101, 106, 110, 114, 117, 119, 121, 124, 127, 127]]

Huge thanks to Sp3000 for helping me out with this challenge in chat

share|improve this question
1  
Your examples seem to have always the same input as ouptut, perhaps you might want to add some where the input is not equal to the output? =) – flawr Mar 24 at 15:59
    
@flawr Whoops, there was a bug in the script I was using to generate the test cases. I've fixed it now. – Mego Mar 24 at 18:23

16 Answers 16

up vote 4 down vote accepted

Jelly, 3 bytes

FṢs

Takes matrix and width as left and right arguments. Try it online!

How it works

FṢs  Main link. Arguments: M (matrix), w (width)

F    Flatten M.
 Ṣ   Sort.
  s  Split into rows of width w.
share|improve this answer

Pyth, 6 5 bytes

1 byte thanks to DenkerAffe

cSsQE

Demonstration

Input is the matrix, followed by its width.

To sort the matrix, I simply flatten it into a list, sort the list, and chop it into a matrix again. Since every subsequence of a sorted list is sorted, this results in a sorted matrix.

share|improve this answer

Retina, 8 6 5 bytes

Thanks to randomra for saving 3 bytes.

O#`.+

Input format is a bit awkward. Both the delimiter within rows and between rows needs to consist of line-terminating characters (i.e. those not matched by .). E.g. you could use single linefeeds within rows and double linefeeds between rows. Output is printed in the same format.

Try it online!

Explanation

The perfect challenge to show off Retina's latest addition: sort stages!

Sort stages work as follows: the regex is matched against the entire string. The matches are then the things that are being sorted, whereas everything around them is treated as "list delimiters" and left unchanged. This challenge can be solved if we treat the input as a flat list, sort that and reshape it into the original matrix form. That's exactly how sort stages work, except there are no explicit steps for flattening and reshaping - Retina just ignores the 2D structure altogether.

As for this code in particular, the regex is .+ which simply matches all runs of non-linefeed characters, which is all the numbers due to the input format. By default, the matches are sorted lexicographically, but the # option tells Retina to sort them numerically (by the first signed integer that appears in the match).

For three additional bytes, this could support any input format whatsoever, by using the regex -?\d+.

share|improve this answer
    
Now I know for sure that Retina is a golfing language. I wasn't sure earlier. Looked totally general-purpose. Well maybe the decimal->unary->decimal stuff was kinda giving it away, but I know now. – CalculatorFeline Mar 24 at 17:24
    
That is awesome. Is it possible to do this without using a builtin sort? – Mama Fun Roll Mar 25 at 5:02
    
@CatsAreFluffy Quoting the docs, "Retina was mainly developed for code golf...". That said I don't understand a) how "golfing language" and "general purpose" language are mutually exclusive, b) how Retina, being based entirely on regex processing, was ever general-purpose in the first place and c) how adding a built-in for sorting makes it any less general-purpose. – Martin Ender Mar 25 at 19:46
    
@MamaFunRoll Sure, Retina is and was Turing-complete. It's going to be pretty painful though. You'd have to convert the integers to unary, and take care of the sign one way or another (e.g. with unary +1 and -1 digits). Then you could bubble sort the list by repeatedly matching pairs x...y with x > y and replacing it with y...x. Then convert unary back to decimal. – Martin Ender Mar 25 at 19:47
    
@MartinBüttner Yeah, that was intended as a joke. – CalculatorFeline Mar 25 at 23:17

Python, 43 bytes

lambda M:eval("map(sorted,zip(*"*2+"M))))")

The interesting question is how to apply mapsort-transpose twice. The shortest I found was to eval a string that has it twice. Compare some alternatives:

lambda M:eval("map(sorted,zip(*"*2+"M))))")
lambda M:map(sorted,zip(*map(sorted,zip(*M))))
s="map(sorted,zip(*%s))";lambda M:eval(s%s%M)
g=lambda M:map(sorted,zip(*M));lambda M:g(g(M))
f=lambda M,n=-1:n*M or f(map(sorted,zip(*M)),n+1)
def f(M):exec"M=map(sorted,zip(*M));"*2;return M

(The second and fourth can be shortened by 1 byte by aliasing sorted, but it's not enough.)

An alternative approach of converting to 1D, sorting, and reshaping to 2D, is very close, at 44 bytes. The width w is taken as an input.

lambda M,w:zip(*[iter(sorted(sum(M,[])))]*w)
share|improve this answer

JavaScript (ES6), 67 bytes

a=>a.map(b=>c.splice(0,b.length),c=[].concat(...a).sort((d,e)=>d-e))

Port of @isaacg's Pyth answer.

share|improve this answer

Pyth, 7 bytes

CSMCSMQ

Try it here!

First we sort all rows of the matrix, transpose the result, sort all rows again and transpose it back.

share|improve this answer

CJam, 8 bytes

{:$z:$z}

Try it online!

An unnamed block which expects a matrix on the stack and leaves the sorted matrix on the stack.

Also just sorts each row (:$), transposes the result (z), sorts again and transposes it back.

share|improve this answer

Octave, 19 bytes

Isn't nice when two programs fulfill the requirement

@(a)sort(sort(a')') % or
@(a)sort(sort(a)')'

Anonymous function that transposes the matrix twice to get the original dimensions back. I'm going to claim that this will be the shortest entry in a non-golfing language. Let's see if anyone proves me wrong :)

share|improve this answer

Mathematica, 26 bytes

Nest[Sort@*Transpose,#,2]&

Function names are long. That sucks.

share|improve this answer
3  
When you have builtins for recognizing upside down goats, it's okay to have long names. :P – Eᴀsᴛᴇʀʟʏ Iʀᴋ Mar 24 at 19:14

APL, 11 9 bytes

⍴⍴⊂∘⍋∘,⌷,

old, bear-looking one:

⍴⍴(⊂∘⍋∘,)⌷,

Hooks of three functions composed together :).

example:

    (⍴⍴⊂∘⍋∘,⌷,) 2 2⍴10 5 4 8
4  5
8 10
share|improve this answer

Python 3, 52 bytes

A solution using numpy

from numpy import*
lambda s:sort(sort(array(s)).T).T

(sort(s,None) is longer as we need to reshape after sort)

Results

>>> f([[-5, -4, -3, -2, -1, 0], [-60, -70, -80, -9, 100, 110], [5, 4, 3, 2, 1, 0], [6, 7, 8, 9, 10, 11]])
array([[-80, -70, -60,  -9,  -1,   0],
       [ -5,  -4,  -3,  -2,   4,   5],
       [  0,   1,   2,   3,  10,  11],
       [  6,   7,   8,   9, 100, 110]])
share|improve this answer

MATL, 4 bytes

S!S!

Try it online!

This just sorts (S) column-wise (sorting the contents of each column seperately) then transposes (!) and sorts the input again, quite straightforward. This program needs a semicolon (;) instead of a comma (,) as delimiter of rows.

share|improve this answer

Ruby, 42 bytes

(m,w)->{m.flatten.sort.each_slice(w).to_a}

Arguments are the matrix and its width. It works exactly like it reads :)

share|improve this answer

RETURN, 10 bytes

[{␇␌}␅␆␎␉]

Try it here.

Anonymous lambda that leaves result on stack. Usage:

""{1 2 3 4}""{1 3 4 2}""{4 1 2 3}""{3 1 2 4}[{␇␌}␅␆␎␉]!

Explanation

[            ]  lambda
 {␇␌}          push matrix width to stack2
      ␅␆      flatten, sort
          ␎␉   chunk by matrix width (popped from stack2)
share|improve this answer

CJam, 6 bytes

{e_$/}

This is a code block (unnamed function) that expects the width and matrix on the stack, pops them and pushes the sorted matrix in return. Try it online!

How it works

     e# STACK: w (width), M (matrix)
     e#
e_   e# Flatten M.
  $  e# Sort the resulting, flat array.
   / e# Split the sorted array into rows of length w.
share|improve this answer

Perl 6, 28 25 bytes

{.flat.sort.rotor(+.[0])}

This takes a list of lists (not arrays).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.