Function Problems

Sort Digits in an Excel Text String in Descending Order

Nimra MalikNimra Malik Nov 18, 2026 869 views

Question details

Sort the individual digits in a text string in descending order and return the result as text.

Sort Digits in an Excel Text String in Descending Order
Product
Microsoft Excel
Device & OS
not provided
Scenario
A cell contains a string of digits that needs to be rearranged from highest to lowest.
Observed behavior
The desired result is a text string such as 221100 for the input 201210, with any zeros retained.
Before you start

Enter the digit string in cell A1. If it begins with zeros, format A1 as Text before entering the value so those zeros are retained.

Solution 1Recommended

Sort the digits with an Excel dynamic array formula

Use this formula in an Excel version that supports LET, SEQUENCE, SORT, and TEXTJOIN.

1
Enter the source string

Type the digits into cell A1, for example 201210.

2
Enter the sorting formula

Select an empty cell and enter =LET(x,SUBSTITUTE(A1,"'",""),TEXTJOIN("",,SORT(MID(x,SEQUENCE(LEN(x)),1),,-1))).

3
Check the result

Press Enter. For 201210, the formula returns 221100 as text.

Sort the digits with an Excel dynamic array formula
Displaying apostrophes: To display literal apostrophes around the result, use =LET(x,SUBSTITUTE(A1,"'",""),"'"&TEXTJOIN("",,SORT(MID(x,SEQUENCE(LEN(x)),1),,-1))&"'"). The apostrophes are then part of the returned text.
Sort digits in WPS Spreadsheets

Sort a digit string as text in WPS Spreadsheets

Use a counting formula in WPS Spreadsheets to arrange digits from 9 to 0 and return them as text.

  1. 1. Open your spreadsheet: Open the workbook in WPS Spreadsheets and enter the digit string in cell A1.
  2. 2. Enter the formula: Select an empty cell and enter =REPT("9",LEN(A1)-LEN(SUBSTITUTE(A1,"9","")))&REPT("8",LEN(A1)-LEN(SUBSTITUTE(A1,"8","")))&REPT("7",LEN(A1)-LEN(SUBSTITUTE(A1,"7","")))&REPT("6",LEN(A1)-LEN(SUBSTITUTE(A1,"6","")))&REPT("5",LEN(A1)-LEN(SUBSTITUTE(A1,"5","")))&REPT("4",LEN(A1)-LEN(SUBSTITUTE(A1,"4","")))&REPT("3",LEN(A1)-LEN(SUBSTITUTE(A1,"3","")))&REPT("2",LEN(A1)-LEN(SUBSTITUTE(A1,"2","")))&REPT("1",LEN(A1)-LEN(SUBSTITUTE(A1,"1","")))&REPT("0",LEN(A1)-LEN(SUBSTITUTE(A1,"0",""))).
  3. 3. Check the sorted text: Press Enter. The formula returns 221100 for 201210; it counts digits and ignores any other characters in A1.
Sort individual digits in descending order without converting the result to a numberRetain zeros included in the source textOpen and save Microsoft Excel XLSX and XLS filesUse familiar spreadsheet formulas and a free Office suite
microsoft office alternative - wps office

Frequently Asked Questions

Does the Excel formula return a number or text?

TEXTJOIN returns text, so the sorted digits remain a text string rather than being converted to a number.

How can I keep leading zeros in the original string?

Before entering the value, select A1 and choose Text from the number format options on the Home tab. Then type the complete string, including its leading zeros.

How do I display apostrophes around the sorted digits?

Add "'"& before TEXTJOIN and &"'" after it in the LET formula. This displays literal apostrophes as part of the result.

What if my Excel version does not support LET or SORT?

Use the REPT and SUBSTITUTE counting formula shown for WPS Spreadsheets in an empty Excel cell. It counts each digit and joins the counts in descending order.