Sort algorithm for Excel / SharedStrings
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When people first inspect an .xlsx file, sharedStrings.xml looks like the obvious place to sort text because it stores many of the workbook's strings. That is usually the wrong mental model. Excel sorts rows and cell values, not the shared-string table in isolation, and the shared-string indexes inside worksheet XML must remain consistent with the string table.
What Shared Strings Actually Do
An .xlsx file is a ZIP container of XML parts. One of those parts can be xl/sharedStrings.xml, which stores deduplicated text values. Cells that contain shared strings do not store the text directly. They store an integer index that points into the shared-string table.
That means two important things:
- the string table is a lookup structure, not the logical row order of the sheet
- reordering shared strings without updating cell indexes corrupts the workbook meaning
So if cell A1 points to shared-string index 5, and you sort the string table but leave the cell index unchanged, A1 now displays the wrong text.
Sort Rows, Not the Shared String Table
If your goal is to sort spreadsheet data, sort the rows in worksheet order and let your library rebuild or preserve string references correctly.
A practical example with openpyxl looks like this:
The workbook library handles the file structure. You sort the business data, not the sharedStrings.xml part directly.
If You Manipulate Open XML Directly
Sometimes you are not using a high-level library. You may be transforming raw Open XML parts. In that case, the safe rule is:
- parse the worksheet rows
- resolve shared-string indexes to actual text when needed for comparison
- sort row records
- write rows back
- either preserve the existing shared-string table or rebuild it together with updated indexes
A simplified example of resolving a shared-string value by index:
That is fine for reading. The dangerous part is writing a modified string table without keeping every referencing cell aligned.
Rebuilding the Shared String Table
If you truly need to regenerate sharedStrings.xml, treat it like a remapping problem. Build a new unique-string list, assign each unique text a new index, and rewrite every shared-string cell in every worksheet to the new index.
That process is more like a normalization pass than a sort pass. The order of strings in the table itself is usually unimportant as long as the references match.
A simple remapping pattern is:
This gives you a deterministic index map, but you still must update all cell references that use those strings.
Choosing the Right Sorting Algorithm
The specific sorting algorithm matters much less than the data model. For ordinary sheet sorting, Python's built-in sorted, Java's Collections.sort, or a library sort is usually enough. The key is sorting row records by resolved values.
Use a stable sort if secondary order matters. For example, when sorting by department but preserving original order among equal departments, stable behavior is helpful.
The real correctness challenge is not whether the algorithm is quicksort or mergesort. It is whether you are sorting the right unit: rows instead of raw shared-string entries.
Common Pitfalls
The biggest mistake is sorting sharedStrings.xml directly and assuming the sheet will still display the same values. It will not unless every string index reference is updated too.
Another mistake is comparing cell XML indexes instead of resolved text values. Shared-string indexes are lookup ids, not alphabetical order.
A third mistake is ignoring rich text and multi-run strings in the string table. Some shared-string entries contain multiple text nodes, so a naive parser can lose formatting or content.
Finally, do not over-engineer the sort itself before fixing the data model. In spreadsheet work, row integrity and reference consistency matter more than the specific comparison algorithm.
Summary
- '
sharedStrings.xmlis a deduplicated lookup table, not the actual sheet sort order' - To sort Excel data, sort worksheet rows and keep shared-string references consistent
- Directly reordering the shared-string table breaks cell meanings unless indexes are remapped everywhere
- High-level libraries such as
openpyxlare safer than raw XML edits for normal sorting tasks - If you rebuild the shared-string table, treat it as a full remapping problem, not a simple lexical sort

