Thursday, 30 January 2025

SORT

 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:

excel
Copy
=SORT(A2:A10)
  • Sorts the range A2:A10 in ascending order.


2. SORT (Descending Order)

Sort a single column in descending order.

Formula:

excel
Copy
=SORT(A2:A10, , -1)
  • The -1 indicates descending order.


3. SORT Multiple Columns

Sort by one column and then by another.

Formula:

excel
Copy
=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:

excel
Copy
=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:

excel
Copy
=SORTBY(A2:C10, B2:B10 * C2:C10, -1)
  • Sorts the range A2:C10 based on the product of columns B and C in descending order.


6. SORT with Conditional Formatting

Sort data and apply conditional formatting.

Steps:

  1. Use =SORT(A2:C10) to sort the data.

  2. Apply conditional formatting to highlight specific values (e.g., values > 100).


7. SORT with Headers

Sort data while keeping headers intact.

Formula:

excel
Copy
=SORT(A1:C10, 2, 1)
  • Sorts the range A1:C10 by column 2 (ascending), including headers.


8. SORT with Unique Values

Sort unique values from a column.

Formula:

excel
Copy
=SORT(UNIQUE(A2:A10))
  • Extracts unique values from A2:A10 and sorts them.


9. SORT with Multiple Criteria

Sort by multiple criteria using SORTBY.

Formula:

excel
Copy
=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:

excel
Copy
=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 SORT function is available in Excel 365 and Excel 2021.

  • It automatically spills results into adjacent cells.

  • Combine SORT with other functions like FILTERUNIQUE, and SORTBY for 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:

excel
Copy
=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) 1 for ascending (default), -1 for descending.

  • by_col: (Optional) FALSE to sort by rows (default), TRUE to 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:

excel
Copy
=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:

ABC
Apple101.5
Banana52.0
Cherry151.0
Date100.5

Formula:

excel
Copy
=SORT(A2:C5, 2, 1, 3, -1)
  • Sorts by column B (ascending), then by column C (descending).

Result:

ABC
Banana52.0
Date100.5
Apple101.5
Cherry151.0

3. SORT with Headers

Sort data while keeping headers intact.

Data:

FruitQtyPrice
Apple101.5
Banana52.0
Cherry151.0
Date100.5

Formula:

excel
Copy
=SORT(A1:C5, 2, 1)
  • Sorts the range A1:C5 by column 2 (Qty) in ascending order, including headers.

Result:

FruitQtyPrice
Banana52.0
Apple101.5
Date100.5
Cherry151.0

4. SORT with Dynamic Arrays

Sort a filtered range dynamically.

Data:

AB
Apple10
Banana5
Cherry15
Date10

Formula:

excel
Copy
=SORT(FILTER(A2:B5, B2:B5 > 5))
  • Filters rows where column B > 5, then sorts the result.

Result:

AB
Apple10
Date10
Cherry15

5. SORT with Calculated Columns

Sort based on a calculated value.

Data:

ABC
Apple101.5
Banana52.0
Cherry151.0
Date100.5

Formula:

excel
Copy
=SORTBY(A2:C5, B2:B5 * C2:C5, -1)
  • Sorts the range A2:C5 by the product of columns B and C in descending order.

Result:

ABC
Cherry151.0
Apple101.5
Date100.5
Banana52.0

6. SORT with Unique Values

Extract and sort unique values.

Data:

A
Apple
Banana
Apple
Cherry

Formula:

excel
Copy
=SORT(UNIQUE(A2:A5))
  • Extracts unique values and sorts them.

Result:

A
Apple
Banana
Cherry

7. SORT with Nested Functions

Combine SORTFILTER, and UNIQUE.

Data:

AB
Apple10
Banana5
Apple15
Cherry10

Formula:

excel
Copy
=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:

AB
Apple15
Cherry10

8. SORT by Columns (Horizontal Sort)

Sort data horizontally (by rows).

Data:

ABCD
1051510

Formula:

excel
Copy
=SORT(A1:D1, , , TRUE)
  • Sorts the range A1:D1 horizontally (by columns).

Result:

ABCD
5101015

9. SORT with Conditional Logic

Sort based on a condition.

Data:

AB
Apple10
Banana5
Cherry15
Date10

Formula:

excel
Copy
=SORT(FILTER(A2:B5, B2:B5 > 5), 2, -1)
  • Filters rows where column B > 5, then sorts by column B in descending order.

Result:

AB
Cherry15
Apple10
Date10

10. SORT with Mixed Data Types

Sort a range with mixed data types (text and numbers).

Data:

AB
Apple10
Banana5
151.0
Date0.5

Formula:

excel
Copy
=SORT(A2:B5, 2, 1)
  • Sorts by column B (ascending), treating numbers and text appropriately.

Result:

AB
Date0.5
151.0
Banana5
Apple10

Key Takeaways:

  • Use SORT to organize data dynamically.

  • Combine SORT with FILTERUNIQUE, and SORTBY for advanced data manipulation.

  • Experiment with sort_indexsort_order, and by_col to customize sorting behavior.


No comments:

Post a Comment