Sort Digits in an Excel Text String in Descending Order
Question details
Sort the individual digits in a text string in descending order and return the result as text.

- 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.
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.
Sort the digits with an Excel dynamic array formula
Use this formula in an Excel version that supports LET, SEQUENCE, SORT, and TEXTJOIN.
Type the digits into cell A1, for example 201210.
Select an empty cell and enter =LET(x,SUBSTITUTE(A1,"'",""),TEXTJOIN("",,SORT(MID(x,SEQUENCE(LEN(x)),1),,-1))).
Press Enter. For 201210, the formula returns 221100 as text.

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. Open your spreadsheet: Open the workbook in WPS Spreadsheets and enter the digit string in cell A1.
- 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. Check the sorted text: Press Enter. The formula returns 221100 for 201210; it counts digits and ignores any other characters in A1.

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.




