Sort 2-D NumPy Array Rows by a Key Column

Quick Overview

Reorder the rows of a 2-D NumPy array so one key column is ascending, keeping each row intact and tied rows in their original order. It tests index permutations, fancy indexing, sort stability and returning a new array without changing the input.

Sort 2-D NumPy Array Rows by a Key Column

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a 2-D NumPy array `arr` and a column index `c`, return a new array with whole rows reordered so that column `c` is ascending. The function must: - Compute the row permutation from column `c` with `np.argsort` and apply it to all rows. - Use `kind='stable'` so rows with equal keys keep their input order. - Leave the input unchanged and return a new array of the same shape. Example: ```python arr = np.array([[3, 100], [1, 200], [2, 300]]) c = 0 # Output: # [[ 1 200] # [ 2 300] # [ 3 100]] ``` ```hint Positions, not values `np.argsort` gives the positions that would sort column `c`. Think about what indexing the full array with those positions along axis 0 does to the other columns, and whether that indexing makes a copy. ``` ### Constraints & Assumptions - `arr` is 2-D with at least one column. - `0 <= c < number of columns`. ### What a Strong Answer Covers - Reordering whole rows so every column stays aligned with its key value. - Why the sort must be stable and what happens to tied keys without it. - Returning a new array while leaving the caller's array unchanged. - Time and space cost in terms of the number of rows and columns. - Edge cases such as an array with zero rows or NaN in the key column. ### Follow-up Questions - How would you sort by column `c` in descending order while keeping tied rows in input order? - How would you break ties in column `c` using a second column? - Where do rows with NaN in a float key column end up, and how would you move them to the front?

Overview: Reorder the rows of a 2-D NumPy array so one key column is ascending, keeping each row intact and tied rows in their original order. It tests index permutations, fancy indexing, sort stability and returning a new array without changing the input.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Given a 2-D NumPy array arr and a column index c, return a new array with whole rows reordered so that column c is ascending.

The function must:

  • Compute the row permutation from column c with np.argsort and apply it to all rows.
  • Use kind='stable' so rows with equal keys keep their input order.
  • Leave the input unchanged and return a new array of the same shape.

Example:

arr = np.array([[3, 100],
                [1, 200],
                [2, 300]])
c = 0

# Output:
# [[  1 200]
#  [  2 300]
#  [  3 100]]

Constraints & Assumptions

  • arr is 2-D with at least one column.
  • 0 <= c < number of columns .

What a Strong Answer Covers Guidance

  • Reordering whole rows so every column stays aligned with its key value.
  • Why the sort must be stable and what happens to tied keys without it.
  • Returning a new array while leaving the caller's array unchanged.
  • Time and space cost in terms of the number of rows and columns.
  • Edge cases such as an array with zero rows or NaN in the key column.

Follow-up Questions Guidance

  • How would you sort by column c in descending order while keeping tied rows in input order?
  • How would you break ties in column c using a second column?
  • Where do rows with NaN in a float key column end up, and how would you move them to the front?