Sort (order) data frame rows by multiple columns
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
When working with data in data frames, it's common to need to sort the data based on the values in one or more columns. Sorting data frame rows by multiple columns can help organize the data more effectively, making it easier to analyze or visualize. This can be done in most data analysis software and programming languages including Python (with pandas), R, SQL, and Excel.
Sorting in Python with pandas
In Python, the pandas library is widely used for data manipulation and analysis. To sort a data frame based on multiple columns, you can use the sort_values() method. Here’s an example:
In the example above, the data frame is first sorted by the column 'Name' in ascending order, and then by 'Age' in descending order where there are ties in 'Name'.
Sorting in R
R is another popular language for data analysis. In R, you can use the order() function combined with the dataframe's square bracket subset method [] for sorting. Here’s how you do it:
In this R code, df$Namesorts by 'Name' in ascending order by default, and-df$Age sorts by 'Age' in descending order.
Sorting in SQL
When working with databases, you can sort multiple columns using SQL queries. Here's an example using the ORDER BY clause:
This SQL query sorts the data by 'name' in ascending order first and then by 'age' in descending order.
Sorting in Excel
Excel users can sort data by multiple columns through the Sort dialog. You must first select the range or table you want to sort, then go to the Data tab and click on Sort. In the Sort dialog, add levels corresponding to each column you wish to sort by, specifying whether the sorting should be ascending or descending for each.
Best Practices and Considerations
- Stability of Sort: Some sorting algorithms are stable, meaning that records with the same key retain their original relative order. This can be particularly relevant when sorting by multiple columns.
- Performance: Sorting can be computationally expensive, especially for large datasets. Choosing efficient sorting algorithms or indexing the data appropriately can help improve performance.
- Type of Data: Different types of data (numeric, text, dates) may require different sorting approaches or syntax, especially if collation or format varies.
| Approach | Function | Example Syntax | Use Case |
| Python | sort_values() | df.sort_values(by=['Name', 'Age']) | Pandas dataframes |
| R | order() | df[order(df$Name, -df$Age), ] | R data frames |
| SQL | ORDER BY | SELECT * FROM table ORDER BY name, age DESC | Database tables |
| Excel | Sort Dialog | Data tab -> Sort | Excel tables or range of cells |
Implementing multiple-column sorting is crucial for efficient data analysis and reporting. Understanding the constraints and options across different tools allows analysts to select the most appropriate sorting methodology for their specific situation.
Related reading
- Sorting algorithms for data of known statistical distribution?
- Sorting an Array in TensorFlow
- Sorting arrays in NumPy by column
- Sorting columns in pandas dataframe based on column name
- sort outer array based on values in inner array, javascript
- Sort points in clockwise order?
- Spark Dataframe write to kafka topic in avro format?
- Spark Find pairs having at least n common attributes?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.