The SORT function in Excel is a powerful tool for organizing data in ascending or descending order. Below are 10 examples of how to use the SORT function, including working with data and formulas.
1. Basic SORT (Ascending Order)
Sort a single column in ascending order.
Formula:
=SORT(A2:A10)
Sorts the range
A2:A10in ascending order.
2. SORT (Descending Order)
Sort a single column in descending order.
Formula:
=SORT(A2:A10, , -1)
The
-1indicates descending order.
3. SORT Multiple Columns
Sort by one column and then by another.
Formula:
=SORT(A2:C10, 2, 1, 3, -1)
Sorts the range
A2:C10:First by column 2 (ascending).
Then by column 3 (descending).
4. SORT with Dynamic Arrays
Sort a dynamic range that expands automatically.
Formula:
=SORT(FILTER(A2:C10, B2:B10 > 50))
Filters rows where column B > 50, then sorts the result.
5. SORT with Formulas
Sort a range based on a calculated column.
Formula:
=SORTBY(A2:C10, B2:B10 * C2:C10, -1)
Sorts the range
A2:C10based on the product of columns B and C in descending order.
6. SORT with Conditional Formatting
Sort data and apply conditional formatting.
Steps:
Use
=SORT(A2:C10)to sort the data.Apply conditional formatting to highlight specific values (e.g., values > 100).
7. SORT with Headers
Sort data while keeping headers intact.
Formula:
=SORT(A1:C10, 2, 1)
Sorts the range
A1:C10by column 2 (ascending), including headers.
8. SORT with Unique Values
Sort unique values from a column.
Formula:
=SORT(UNIQUE(A2:A10))
Extracts unique values from
A2:A10and sorts them.
9. SORT with Multiple Criteria
Sort by multiple criteria using SORTBY.
Formula:
=SORTBY(A2:C10, B2:B10, 1, C2:C10, -1)
Sorts
A2:C10:First by column B (ascending).
Then by column C (descending).
10. SORT with Nested Formulas
Combine SORT with other functions like FILTER and UNIQUE.
Formula:
=SORT(UNIQUE(FILTER(A2:C10, B2:B10 > 50)), 2, -1)
Filters rows where column B > 50, extracts unique values, and sorts by column 2 in descending order.
Key Notes:
The
SORTfunction is available in Excel 365 and Excel 2021.It automatically spills results into adjacent cells.
Combine
SORTwith other functions likeFILTER,UNIQUE, andSORTBYfor advanced data manipulation.
Let’s dive deeper into the SORT function in Excel, exploring its syntax, advanced use cases, and how it interacts with other functions. I’ll also include examples with data and formulas to help you master this powerful tool.
Understanding the SORT Function
Syntax:
=SORT(array, [sort_index], [sort_order], [by_col])
array: The range of data to sort.sort_index: (Optional) The column or row index to sort by. Default is 1 (first column/row).sort_order: (Optional)1for ascending (default),-1for descending.by_col: (Optional)FALSEto sort by rows (default),TRUEto sort by columns.
In-Depth Examples
1. Basic SORT (Single Column)
Sort a single column in ascending order.
Data:
| A |
|---|
| Apple |
| Banana |
| Cherry |
| Date |
Formula:
=SORT(A2:A5)
Result:
| Sorted Data |
|---|
| Apple |
| Banana |
| Cherry |
| Date |
2. SORT with Multiple Columns
Sort a table by one column, then by another.
Data:
| A | B | C |
|---|---|---|
| Apple | 10 | 1.5 |
| Banana | 5 | 2.0 |
| Cherry | 15 | 1.0 |
| Date | 10 | 0.5 |
Formula:
=SORT(A2:C5, 2, 1, 3, -1)
Sorts by column B (ascending), then by column C (descending).
Result:
| A | B | C |
|---|---|---|
| Banana | 5 | 2.0 |
| Date | 10 | 0.5 |
| Apple | 10 | 1.5 |
| Cherry | 15 | 1.0 |
3. SORT with Headers
Sort data while keeping headers intact.
Data:
| Fruit | Qty | Price |
|---|---|---|
| Apple | 10 | 1.5 |
| Banana | 5 | 2.0 |
| Cherry | 15 | 1.0 |
| Date | 10 | 0.5 |
Formula:
=SORT(A1:C5, 2, 1)
Sorts the range
A1:C5by column 2 (Qty) in ascending order, including headers.
Result:
| Fruit | Qty | Price |
|---|---|---|
| Banana | 5 | 2.0 |
| Apple | 10 | 1.5 |
| Date | 10 | 0.5 |
| Cherry | 15 | 1.0 |
4. SORT with Dynamic Arrays
Sort a filtered range dynamically.
Data:
| A | B |
|---|---|
| Apple | 10 |
| Banana | 5 |
| Cherry | 15 |
| Date | 10 |
Formula:
=SORT(FILTER(A2:B5, B2:B5 > 5))
Filters rows where column B > 5, then sorts the result.
Result:
| A | B |
|---|---|
| Apple | 10 |
| Date | 10 |
| Cherry | 15 |
5. SORT with Calculated Columns
Sort based on a calculated value.
Data:
| A | B | C |
|---|---|---|
| Apple | 10 | 1.5 |
| Banana | 5 | 2.0 |
| Cherry | 15 | 1.0 |
| Date | 10 | 0.5 |
Formula:
=SORTBY(A2:C5, B2:B5 * C2:C5, -1)
Sorts the range
A2:C5by the product of columns B and C in descending order.
Result:
| A | B | C |
|---|---|---|
| Cherry | 15 | 1.0 |
| Apple | 10 | 1.5 |
| Date | 10 | 0.5 |
| Banana | 5 | 2.0 |
6. SORT with Unique Values
Extract and sort unique values.
Data:
| A |
|---|
| Apple |
| Banana |
| Apple |
| Cherry |
Formula:
=SORT(UNIQUE(A2:A5))
Extracts unique values and sorts them.
Result:
| A |
|---|
| Apple |
| Banana |
| Cherry |
7. SORT with Nested Functions
Combine SORT, FILTER, and UNIQUE.
Data:
| A | B |
|---|---|
| Apple | 10 |
| Banana | 5 |
| Apple | 15 |
| Cherry | 10 |
Formula:
=SORT(UNIQUE(FILTER(A2:B5, B2:B5 > 5)), 2, -1)
Filters rows where column B > 5, extracts unique values, and sorts by column 2 in descending order.
Result:
| A | B |
|---|---|
| Apple | 15 |
| Cherry | 10 |
8. SORT by Columns (Horizontal Sort)
Sort data horizontally (by rows).
Data:
| A | B | C | D |
|---|---|---|---|
| 10 | 5 | 15 | 10 |
Formula:
=SORT(A1:D1, , , TRUE)
Sorts the range
A1:D1horizontally (by columns).
Result:
| A | B | C | D |
|---|---|---|---|
| 5 | 10 | 10 | 15 |
9. SORT with Conditional Logic
Sort based on a condition.
Data:
| A | B |
|---|---|
| Apple | 10 |
| Banana | 5 |
| Cherry | 15 |
| Date | 10 |
Formula:
=SORT(FILTER(A2:B5, B2:B5 > 5), 2, -1)
Filters rows where column B > 5, then sorts by column B in descending order.
Result:
| A | B |
|---|---|
| Cherry | 15 |
| Apple | 10 |
| Date | 10 |
10. SORT with Mixed Data Types
Sort a range with mixed data types (text and numbers).
Data:
| A | B |
|---|---|
| Apple | 10 |
| Banana | 5 |
| 15 | 1.0 |
| Date | 0.5 |
Formula:
=SORT(A2:B5, 2, 1)
Sorts by column B (ascending), treating numbers and text appropriately.
Result:
| A | B |
|---|---|
| Date | 0.5 |
| 15 | 1.0 |
| Banana | 5 |
| Apple | 10 |
Key Takeaways:
Use
SORTto organize data dynamically.Combine
SORTwithFILTER,UNIQUE, andSORTBYfor advanced data manipulation.Experiment with
sort_index,sort_order, andby_colto customize sorting behavior.
No comments:
Post a Comment