Sort By Multiple Keys

HardData EngineeringArraySortingMatrix

Description

Given a 2D array of rows, sort the rows ascending by the first column, breaking ties by the second column, then the third, and so on across all columns.

Examples

Input:[[2,1],[1,3],[1,2],[2,0]]
Output:[[1,2],[1,3],[2,0],[2,1]]
Explanation:

Rows are ordered by their first column, and any ties are broken by comparing the following columns in turn.

Input:[[3],[1],[2]]
Output:[[1],[2],[3]]
Explanation:

Rows are ordered by their first column, and any ties are broken by comparing the following columns in turn.

Input:[[1,2,3],[1,2,1]]
Output:[[1,2,1],[1,2,3]]
Explanation:

Rows are ordered by their first column, and any ties are broken by comparing the following columns in turn.

Constraints

  • 1 ≤ rows ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.