6. Tables, Sorting and Filtering
6.4 Sorting: Multi-level and Custom Sort
Design the order that helps someone find a record
A fictional Blinkit-inspired report has orders from three cities. A reviewer wants to see one city together, with larger amounts first and older orders first when amounts tie. Sorting one column of amounts can make the numbers look orderly while disconnecting them from their order IDs. Instead, choose a sequence of sort rules for whole records.
The order IDs, cities, dates, amounts and service states in this exercise are invented. They illustrate the worksheet operations and do not describe any real company's order or review process. Save a small untouched copy of the original dataset before sorting. It gives you a clear way to compare the original shape and restore it.
Compare a sort with a filter
A sort rearranges records. A filter changes which records are currently visible. If the team asks to see only Pune, they need a row selection. If they want every city grouped and ordered, they need a sort. Both operations can coexist, but first work with all rows visible so you can verify the full ordering.
Read the sort levels in priority order
- Primary key. This field decides the first grouping or order. City first keeps orders from the same city together.
- Secondary key. This applies within records sharing the first key. Amount largest to smallest works inside each city cluster.
- Further ties. If both city and amount match, compare the date. If the date is also the same, compare a stored import sequence so that the order is reproducible.
Prepare a worksheet that sorts cleanly
Make a new tab named SortPractice. This lesson uses its own small fixture because it needs repeated city labels, exact amount ties and dates that resolve some of those ties. Create the fields in A through F. Keep notes, summaries and test values outside the record rectangle.
| A Order ID | B City | C Amount | D Order Date | E Status | F Import sequence |
|---|---|---|---|---|---|
| DS-501 | Pune | 150 | 4 Nov 2026 | Delivered | 1 |
| DS-502 | Nashik | 200 | 3 Nov 2026 | Returned | 2 |
| DS-503 | Pune | 220 | 5 Nov 2026 | Cancelled | 3 |
| DS-504 | Pune | 150 | 3 Nov 2026 | Delivered | 4 |
| DS-505 | Nashik | 200 | 2 Nov 2026 | Delivered | 5 |
| DS-506 | Pune | 150 | 3 Nov 2026 | Returned | 6 |
| DS-507 | Nagpur | 240 | 2 Nov 2026 | Cancelled | 7 |
Check the fields before changing their order
- Amount. Type 150 as a number. Apply a display format after entry if helpful. A value that looks like 150 can still be a stored string. Numeric sort and text order are different decisions.
- Order date. Use real date values. For example, enter
=DATE(2026,11,4)for DS-501 if your locale makes typed dates ambiguous. A displayed date string can look identical to a real date but sort as text. - Sequence. Store ordinary numbers 1 through 7 in the original input order. A formula based just on the current row number would recalculate from the new location and does not serve as a fixed recovery key.
- Header row. Include the heading cells in the selected rectangle while telling the Sort dialog that the data has headers. Check names such as Order Date appear as choices. If the dialog shows only a column letter or an unexpected label, inspect the selection.
- Hidden rows and filters. Clear active filters and inspect any hidden rows or columns before practising a whole-list sort. A selected visible region can leave out records you intended to include.
Keep a baseline numeric amount of ₹1,310 and a record count of seven. Later, matching these figures confirms the amount set and population size. Also keep the original record details, because the same sum and size cannot prove that every amount still belongs to the right ID.
Lab 1 · Sort by city, amount, date and sequence
Use the original seven records in a copy. Select the complete data rectangle A1:F8 or work within a correctly defined Table containing all six fields. This exercise uses the ordinary worksheet Sort dialog. Desktop and web labels can differ. Microsoft's current instructions include multi-level Custom Sort in supported web clients, with some sort options differing from desktop.
Set up the levels in one operation
- In the Data tab open Sort or Custom Sort. Confirm the header option and the full record population. Set the first field to City and the order to ascending.
- Add a level for Amount and choose largest to smallest. This second key rearranges amounts within each city, without moving a Pune order into another city's grouping.
- Add Order Date ascending, meaning oldest to newest. Within an equal-city and equal-amount group, the earlier date comes first. Add Import sequence ascending as the final key. It gives a chosen answer for records tied on the earlier fields.
- Apply the sort. Read the ordered IDs from the whole worksheet. In the English-language example, Nagpur precedes Nashik and both precede Pune. Check the within-group order rather than relying on the top cell alone.
| Position | ID | City | Amount | Order Date |
|---|---|---|---|---|
| 1 | DS-507 | Nagpur | 240 | 2 Nov |
| 2 | DS-505 | Nashik | 200 | 2 Nov |
| 3 | DS-502 | Nashik | 200 | 3 Nov |
| 4 | DS-503 | Pune | 220 | 5 Nov |
| 5 | DS-504 | Pune | 150 | 3 Nov |
| 6 | DS-506 | Pune | 150 | 3 Nov |
| 7 | DS-501 | Pune | 150 | 4 Nov |
Recheck DS-503's ₹220 and Cancelled status after it moves. Check DS-505's date and amount. Its ₹200 ties the other Nashik order, and its earlier date explains its new position. Sum the original Amount field again and verify ₹1,310. You have now checked both the intended order and the unchanged record identity.
Trace the fixtures that test each level
You cannot confirm a multi-level sort from one unusually large record. To test the second and later keys, you need ties in the earlier fields. The Pune and Nashik groups deliberately include them. Start from those groups to explain why the final output was produced.
Follow the Pune ties through the sort keys
Find the group
Four records have City Pune. The first level keeps them together after Nagpur and Nashik in the English example.
Order amounts within the group
DS-503 has ₹220, larger than the other three at ₹150. It leads Pune even though its recorded date is later.
Compare dates only inside the amount tie
Two ₹150 orders have 3 November dates, and one has 4 November. The two earlier orders precede DS-501.
Use sequence to resolve the remaining tie
DS-504 and DS-506 have equal city, amount and date. Sequence four comes before sequence six. Their statuses still belong to their own records.
To see the priority effect, make another scratch copy and place Order Date before Amount within each city. On that copy, the Pune orders on 3 November now lead the Pune group. DS-503 with ₹220 arrives later in the date-first layout because its date is 5 November. The instructions changed. The amounts did not. Choose which sort key the reader needs first.
Use the stored Import sequence to return a working copy to its original arrangement. An opposite alphabetical sort reverses a city grouping. It does not recreate the original interleaved input sequence. A sequence field protects you from trying to infer source order from dates that can be identical.
Lab 2 · Use a business status order
Use another copy of the seven orders. This time the reviewer wants Delivered first, Returned second and Cancelled last. The order is a fictional classroom reporting rule. Alphabetical sort would order these status labels differently. Use a custom list where the client supports it, or create an explicit numeric priority field that can be verified from cells.
Apply a custom list in a supported desktop client
- Write the three labels as a reference list in the intended order. Spell each exactly as it appears in the source. You can use the desktop custom-list settings to create or import the list. Microsoft documents the Windows route under Excel Options and the Mac route under Excel preferences.
- Sort the full record block. Set Status as the first level. Under Order choose the custom list Delivered, Returned, Cancelled. If the client does not provide this option, use the numeric helper below.
- Add Amount from largest to smallest, Order Date oldest to newest, and Import sequence ascending. These choices operate within each equal status group. They still need to be specified when you use a custom list.
- Check the order against the table. Confirm the first Delivered order is DS-505 and the first Returned order is DS-502. Both hold ₹200. The primary Status order puts all Delivered records ahead of the Returned group.
| Position | ID | Status | Amount |
|---|---|---|---|
| 1 | DS-505 | Delivered | 200 |
| 2 | DS-504 | Delivered | 150 |
| 3 | DS-501 | Delivered | 150 |
| 4 | DS-502 | Returned | 200 |
| 5 | DS-506 | Returned | 150 |
| 6 | DS-507 | Cancelled | 240 |
| 7 | DS-503 | Cancelled | 220 |
Use a numeric helper where the intended order belongs in the workbook
In a new column G put Status priority. With Status in E, enter =IF(E2="Delivered",1,IF(E2="Returned",2,IF(E2="Cancelled",3,99))) in G2 and fill it down. It assigns the three known statuses to numeric ranks. A number of 99 flags a label outside this practice code list and places it after the known groups when sorted ascending. This decision is visible in the sheet rather than depending on a saved custom list on a specific client.
Sort by the numeric helper ascending as the primary level, followed by the same three secondary levels. Check that the seven IDs match the result above. In a practice copy, change DS-505 from Delivered to Awaiting and verify its rank is 99. Check that DS-505 moves to the end under this rule. Decide whether to keep unexpected labels there or stop for review in a real report. Restore the seeded status after the test.
Use the displayed order for the actual reader
Custom sorting helps express a known business sequence even when the text labels do not have a natural alphabetical order. The same idea applies to a reporting week that starts on Monday or sizes that progress from small to large. Microsoft provides certain built-in weekday and month lists, and their starting order can differ from your use case. Check the list you choose rather than relying on a word like “weekday” to imply the starting day.
When sharing a workbook, preserve the intended order in a visible reference list or numeric priority field if collaborators might need to recreate it in another environment. A saved layout that already appears sorted can be useful. A verified sort rule and a stored priority make it easier to sort additional records in the same order.
Create a sorted view without rearranging the source
The worksheet Sort command changes where the source records appear. A formula such as SORTBY creates another ordered set of values in a separate destination. This helps when you want to preserve an original entry order and show a continuously updated presentation view next to it.
Use a client that supports SORTBY. Microsoft's current documentation lists Microsoft 365, Excel for the web, Excel 2024 and Excel 2021 among supported versions. Work outside a Table when the formula needs to spill into several cells. A spill is the area occupied by a formula returning multiple values.
Match the whole output array and its key arrays
- Begin from the untouched seven-record copy with complete source range A2:F8. Leave H2:M8 clear for seven rows and six output columns. Place headings in row 1 of that area if you want the same readable labels.
- Enter the expression below in H2. The first range is the complete data to return. The remaining pairs give each key range and its direction. Here 1 means ascending and -1 means descending.
=SORTBY(A2:F8,B2:B8,1,C2:C8,-1,D2:D8,1,F2:F8,1)
- Check that each sort key lists one value for each source row in the same order. B supplies city, C amount, D date and F sequence. An equal-size key range that starts one row too late would still associate the wrong keys with the records.
- Confirm the output IDs match the expected Lab 1 order. At its original location A2, the unchanged source still starts with DS-501. The first ordered output in H2 is DS-507. That difference is the useful feature of this formula design.
- Change the ₹220 amount in DS-503 to 140 in a scratch copy. Its Pune ordering changes. The three ₹150 records now precede it because the numeric key changed. Restore 220 and confirm its position in the Pune group returns.
Know when the sorted view can grow
The stated source address ends at row 8. Typing a new order below it does not automatically cause that fixed formula to read another row. If the report should follow a particular Table's growth, consider referencing its data and key columns by name and leave enough output space. Test the effective array shape after an append. A blocked output or a changed source boundary calls for a different repair from a wrong key direction.
A command-based sort is useful when the working record order itself should change. A formula view is useful when that order needs to coexist with the untouched source. Both require the same clear definition of which data to include and which keys mark the intended order.
Practice extension · reorder columns in an ordinary range
Most order data uses one record per row, so sorting moves whole rows up and down. Some presentation grids instead store one observation set per column. Excel supports a left-to-right sort in suitable clients for an ordinary range. Keep this operation separate from a normal Excel Table, which does not support left-to-right sorting.
Use a small checked quarter report
Open a scratch tab named QuarterView. Put Quarter, Amount and Units in A1 through A3 as row labels. In B1:D1 put Q3, Q1 and Q2. In B2:D2 put 300, 100 and 200. In B3:D3 put 30, 10 and 20. The Q3 column contains both 300 and 30; Q1 contains 100 and 10; Q2 contains 200 and 20.
- Select B1:D3, leaving the independent row labels in column A outside the selected range. This is a different layout from an order table. Its comparable records are columns, and their three values must move together.
- In a supported Sort dialog, open Options and choose the left-to-right orientation. Choose row 1 as the sorting key and sort its quarter labels ascending. If the control is absent in your client, use a supported desktop client or manually copy the three complete columns to a new practice view in the intended order.
- Verify B1:D1 now reads Q1, Q2, Q3, with amounts 100, 200, 300 and units 10, 20, 30. The quarter labels and both measures have moved together. The row headings in A remain in place.
- Check the sum of the three amounts is still 600 and the sum of the units is still sixty. Then read the Q1 amounts and units to verify the pairing. A sum alone would not catch reordering the quarter labels while leaving their measures fixed.
If the source is a Table and its column order needs deliberate reorganization, inspect the supported controls for rearranging fields, or build a separate ordinary range copy for a left-to-right sort. Converting a working Table can change formulas that refer to it. Preserve the modeled source in the practice example by making the alternative layout separately.
Use color and icons as deliberate display criteria
In a supported Excel desktop client, Sort can order using a cell's fill color, font color or a conditional-formatting icon. These are display properties. The sort requires you to select the color or icon and where those records should appear. There is no universal sequence for these properties. Set each required level explicitly and verify it against the visible group.
Try a scratch copy of SortPractice and mark the status cells for DS-503 and DS-507 with the same practice fill. Ask the Sort dialog to place that fill at the top using Status's cell color. Both matched rows should lead the report if the client provides the required control. Give them a secondary Amount-descending level, and the ₹240 DS-507 record should precede the ₹220 DS-503 record. Other rows follow according to the remaining active sort keys.
Conditional formatting can change when underlying values change. If your sorting rule is based on its displayed icon or fill, check the actual result after a recalculation or update. A sorted view does not always reorder itself as soon as a source formula changes; a command-based sort may need to be reapplied.
Catch a selection that separates a record from its fields
Consider the original Amount values and the seven unchanged ID cells. If you arrange just the numbers from largest to smallest, ₹240 moves to the first amount position. If the IDs stay in their original order, DS-501 would appear to have ₹240 even though its real amount is ₹150. The total remains ₹1,310 and the numeric count still shows seven. This is a visible example of why totals cannot detect all selection errors.
Read the warning before confirming a subset sort
- Select the intended record area in full. In a normal range, if Excel asks whether to expand a partial selection, review the expanded region and keep the complete records together. Cancel and select the exact input block if the proposal includes unrelated cells.
- Remember that an intentional independent one-column list can be sorted by itself. An Amount column attached to order IDs is not an independent list. Use the record definition as your selection rule.
- After sorting, read a known moved record's ID, city, amount, date and status. Compare it against the untouched copy. The named sequence should still be associated with that record.
- If the wrong subset was sorted on the practice sheet, Undo immediately when available. If later edits make Undo unsuitable, use the original confirmed dataset as evidence to restore the correct pairings. Sorting the worksheet again by city cannot reconstruct those relationships after some fields have been detached.
A properly defined Table is useful because a normal Table sort acts on its records. Verify that all required fields are actually Table members. An Order ID in the Table and a separate manually maintained status column beside it can still lose their relationship when only the Table is sorted. Include every field that belongs to each record in the same source or connect separately maintained data by a verified key.
Diagnose a sort with mixed input types
A worksheet can display a numeric-looking value as text and still place it into a different group during sorting. Imported values can carry an apostrophe, ordinary spaces, uncommon characters or punctuation that does not match the locale. Sorting by magnitude requires input that Excel recognizes as numeric.
Test numeric and text cases with the same display
In a scratch copy, make one amount display 150 while being stored as text. Enter an apostrophe before 150 for the controlled test. Compare =ISNUMBER(C2) with a known numeric cell, using the actual row where you placed the string. The display can look the same while the type check gives FALSE for the text input. Re-enter the verified amount as a number before relying on the numeric ordering of the report.
Run the same thinking on dates. When a colleague pastes a timestamp that is text, changing only the display format may not convert the existing value. Confirm the expected date components and the stored type. Type it with DATE when testing a known date-only value. A report that must sort true timestamps needs a consistent source representation for both day and time.
Check when the two reports use different collation rules
Text ordering can depend on language and region settings. If a report needs a specific priority for codes or cities, write an explicit custom list or helper rank. If the sort needs a case-sensitive distinction, verify that the chosen client exposes that option and test a known pair of labels. An ordinary alphabetical grouping may treat letter case differently from an explicitly case-sensitive source classification.
Decide whether missing values should be left alone or brought to a review area. An empty date can have a different meaning from an early date. Turning missing dates into arbitrary old dates just to put them at the start changes the data. Add a named review or priority field if the intended order needs to separate incomplete rows.
Make a sorting rule understandable to the next reader
When a colleague asks for a changed order, write the complete key sequence and directions. A phrase like “by date and amount” does not tell the learner what happens when they conflict. Say City ascending, Amount descending, Date ascending, and Import sequence ascending. That description makes it possible to choose a source fixture that tests each level.
- Count the original records and preserve the initial sequence as numbers. Select the whole intended range or verify the Table fields.
- Choose a sample with different cities. It tests the primary key. Pick two amounts within the same city to test the next key.
- Choose equal amounts in the same city with different dates. Then choose a pair with the same date as well, so the last key gets tested. Compare the exact ordered IDs.
- Check what a source edit would do. If the earlier city or amount changes, the later tie-breakers may no longer apply. For command-based sort, inspect whether the order needs to be reapplied after the edit.
Another useful comparison uses the original sample with Amount as the primary key. The first two records are DS-507 at ₹240 and DS-503 at ₹220. The city-first report happens to put DS-507 first too, so the top row alone cannot distinguish the two rules. The second row tells them apart. It is the ₹200 DS-505 order under the city-first sequence. This is why verifying a couple of hard cases adds more information than only checking the top value.
Use the same audit when the workbook is shared with a teammate. Record the input version, the expected number of visible or total records, and the first few IDs expected under the stated sort. If this is a filtered queue, write its filters separately so someone reproducing the order knows which rows they should compare.
Choose a lasting final key
In the practice list each Import sequence value is unique. If two incoming batches each start numbering at one, those values alone may no longer define a final order across batches. Assign a distinct sequence for the combined dataset or use a batch field followed by a within-batch sequence according to the approved order. Verify one pair that had the same amount and date in different batches.
An order ID can work as a final tie-breaker when the report deliberately uses its ordering. For example, in a pair of tied values DS-504 can come before DS-506 by text order. A numeric sequence is better evidence of input order when the IDs do not encode it. Record the purpose of the final key, since different valid tie rules can produce different visible orders from the same rows.
Test two cases with the same city and amount and different dates. Then copy a record's date into the other case in a scratch copy so the sequence decides. For DS-504 and DS-506, temporarily move DS-506's date to 2 November. It then precedes DS-504 within the ₹150 Pune group even though its import sequence is larger. Restore the original date and check that the original tie order returns.
A final key only resolves ties. Moving its level above Order Date would make the sequence determine the order before Excel considers the dates. The sort dialog's level order is part of the definition. Keep a note of that order when another reader must reproduce the report.
When using a helper that ranks categories, test its output before sorting. The priority must be a numeric field and should move with the same record as its status. Sort all selected members, including that helper column.
Practice extension · use a city order that differs from the alphabet
Set a fictional daily route order of Pune first, Nashik second and Nagpur third. A custom list can keep those cities together in the intended workflow. The three groups still need instructions for their internal ordering. Use Amount descending, then Order Date and Import sequence ascending, so each city follows the same report rules as Lab 1.
- In a scratch copy, use the custom list or a numeric mapping with Pune at one, Nashik at two and Nagpur at three. Apply it to the full record set. The stated order uses the same groups as Lab 1, with a different first ordering decision.
- Expect DS-503, DS-504, DS-506 and DS-501 as the first four IDs. The order within the Pune group stays the same. Nashik follows with DS-505 then DS-502. Nagpur is last with DS-507, even though its ₹240 is the largest amount in the complete dataset.
- Test a new city label in a helper version. A clear fallback rank can put an unfamiliar city after the known ones, and a separate review flag can show why. Pick a source label and test its exact placement. Revisit the business order before adding a new approved group.
- Make a sort description that explicitly names the city list. “City ascending” would mislead another reader because Pune should come first in this workflow. “City route order” with a visible mapping gives them the rule they need.
The outcome depends on record values and on a chosen ordering. Neither changing the first key nor changing the priority list alters the original amount attached to any order. Check both facts when a teammate asks why a high amount appears later in a route report.
Quiz · predict the position of a known record
Which key decides that all Pune records stay in one group?
The City key is the first sort level. Inside Pune, the later amount, date and sequence rules determine order. A larger order in another city cannot move into that group.
Why does DS-503 lead the Pune group despite its later date?
Amount comes before date in the key list. It has ₹220 and the other Pune orders have ₹150. Dates decide only within an earlier city-and-amount tie.
What resolves the tie between DS-504 and DS-506?
They share city, amount and order date. Their stored import sequences differ. Sequence four precedes sequence six, giving a reproducible final order.
Could a wrong partial sort still show the original amount total?
Yes. Reordering only amounts can leave the set of amounts and its sum unchanged while changing which ID is next to each amount. Compare complete records against an untouched source.
Why is a numeric status priority useful?
It gives Excel a visible sequence to sort in ascending order. It can also assign a separate position to unexpected statuses. This reproduces the intended business order without needing a specific custom-list control.
What stays fixed when SORTBY returns a view of the seven original source rows?
The source stays in its original input locations. The formula returns an ordered output elsewhere. Verify the complete output shape and matching key ranges.
Interview practice · describe how you would verify a sort
How do you check the second sort level actually took effect?
I would use several rows with the same primary key and different secondary values. For example, multiple Pune orders reveal whether the amounts are descending within that group. I would add tied amounts with varied dates to verify the later level.
What field would you use to restore the original import order?
I would keep an independent numeric sequence recorded as part of each import row. I would verify it stays tied to the same ID. A reverse sort by city or date does not reconstruct mixed original rows with tied dates.
How would you handle values that look numeric but sort unexpectedly?
I would inspect the stored types and confirm the intended values. An imported 150 can be text. A type check and a small expected-order fixture help me verify conversion before using a numeric sort.
When would you use a formula sorted view?
I would use it where the source input order must be preserved and a separate ordered presentation can recalculate. I would check that the arrays cover the same records and the spill has enough room.
What would you inspect before using a custom order across clients?
I would record the intended category sequence and check the supported controls. A numeric rank field offers a visible workbook-based method when the clients handle saved custom lists differently. I would compare the exact record IDs in both outputs.
Six cards for a reproducible sort
- Whole record Include every field that should travel with each order.
- Primary key Controls the main grouping. Earlier levels take priority.
- Tie-breaker Tests a later field only after earlier values match. Store a final key when reproducibility matters.
- Custom order List category priorities or provide a numeric helper with an explicit review value.
- Value types Verify numeric and date fields before sorting by their actual values.
- Proof Compare exact IDs and matched field values, then check the expected population and totals.
Continue with AutoFilter when the goal is to narrow the visible rows. The chapter overview shows how the same Table can preserve a grand total while its view changes.
Microsoft references
Continue practising: explore the existing Excel Task Hub for additional exercises.