Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Sorting Arrays with np.sort()
📖 Scenario: You work in a small shop that tracks daily sales numbers. You want to organize the sales data from smallest to largest to see trends easily.
🎯 Goal: Learn how to use np.sort() to sort a list of daily sales numbers in ascending order.
📋 What You'll Learn
Create a NumPy array with exact daily sales numbers
Use a variable to hold the sorted array
Use np.sort() to sort the array
Print the sorted array
💡 Why This Matters
🌍 Real World
Sorting sales data helps businesses quickly understand trends and make decisions.
💼 Career
Data scientists often sort data to prepare it for analysis and visualization.
Progress0 / 4 steps
1
Create the sales data array
Create a NumPy array called sales with these exact daily sales numbers: 250, 100, 300, 150, 200.
NumPy
Hint
Use np.array() and put the numbers inside a list.
2
Prepare a variable for sorted sales
Create a variable called sorted_sales and set it to None for now.
NumPy
Hint
Just write sorted_sales = None to create the variable.
3
Sort the sales array using np.sort()
Use np.sort() to sort the sales array and assign the result to sorted_sales.
NumPy
Hint
Call np.sort(sales) and save it in sorted_sales.
4
Print the sorted sales array
Print the sorted_sales array to see the sales numbers sorted from smallest to largest.
NumPy
Hint
Use print(sorted_sales) to display the sorted array.
Practice
(1/5)
1. What does the np.sort() function do when applied to a NumPy array?
easy
A. It returns a new array with elements sorted in ascending order.
B. It changes the original array to sorted order in place.
C. It reverses the order of elements in the array.
D. It removes duplicate elements from the array.
Solution
Step 1: Understand np.sort() behavior
The np.sort() function returns a new sorted array and does not modify the original array.
Step 2: Identify the sorting order
By default, np.sort() sorts elements in ascending order.
Final Answer:
It returns a new array with elements sorted in ascending order. -> Option A
Quick Check:
np.sort() returns sorted copy [OK]
Hint: np.sort() returns a new sorted array, original stays same [OK]
Common Mistakes:
Thinking np.sort() sorts in place
Confusing sorting with reversing
Assuming it removes duplicates
2. Which of the following is the correct syntax to sort a 1D NumPy array named arr using np.sort()?
easy
A. np.sort(arr)
B. arr.sort()
C. np.sort(arr, axis=1)
D. arr.sorted()
Solution
Step 1: Recall np.sort() syntax
The correct way to sort an array using the function is np.sort(arr).
Step 2: Check other options
arr.sort() sorts in place but is a method, not np.sort(). np.sort(arr, axis=1) is invalid for 1D arrays. arr.sorted() is not a valid method.
Using axis=1 on a 1D array causes an error because axis 1 does not exist.
Final Answer:
Axis 1 does not exist for 1D arrays. -> Option D
Quick Check:
1D array has only axis 0 [OK]
Hint: 1D arrays only have axis=0, axis=1 causes error [OK]
Common Mistakes:
Assuming axis=1 works on 1D arrays
Thinking np.sort can't handle integers
Believing array must be list to sort
5. Given a 2D NumPy array data = np.array([[7, 2, 9], [4, 5, 1], [8, 3, 6]]), how can you sort the entire array as if it were a flat list, then reshape it back to the original shape?
hard
A. np.sort(data, axis=0).reshape(data.shape)
B. np.sort(data, axis=None).reshape(data.shape)
C. data.sort(axis=1).reshape(data.shape)
D. np.sort(data).reshape(data.shape)
Solution
Step 1: Flatten and sort the entire array
Using axis=None in np.sort() sorts the array as a flat 1D array.
Step 2: Reshape sorted array back to original shape
Use .reshape(data.shape) to restore the 2D shape after sorting.
Final Answer:
np.sort(data, axis=None).reshape(data.shape) -> Option B
Quick Check:
axis=None sorts flat, reshape restores shape [OK]
Hint: Use axis=None to sort flat, then reshape [OK]