Sorting numbers Assending or descending within a cell

Kamran01

Board Regular
Joined
Feb 10, 2017
Messages
86
Dear Experts,

I have a cell, in which a series of numbers available. I want to sort these numbers assending and descending orders.
Can anyone suggest a formula to do this.

Cell Values Assending / Desending
5573914 1345579 9755431
4513217 1123457 7543211
5373478 3345778 8775433
1563957 1355679 9765531

Thanks,
Kamran Noor
 
I cannot think of any way to do this with a regular formula (which is why I posted the UDF for you to use).

Here is the regular formula solution

1] Ascending sort digits :
=TEXT(SUMPRODUCT(AGGREGATE(14,6,--MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1),ROW(INDIRECT("1:"&LEN(A2))))*10^(ROW(INDIRECT("1:"&LEN(A2)))-1)),REPT("0",LEN(A2)))

2] Descending sort digits :
=TEXT(SUMPRODUCT(AGGREGATE(15,6,--MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1),ROW(INDIRECT("1:"&LEN(A2))))*10^(ROW(INDIRECT("1:"&LEN(A2)))-1)),REPT("0",LEN(A2)))

Regards
Bosco
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this:

Sheet1

ABC

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:109px;"><col style="width:103px;"><col style="width:95px;"></colgroup><tbody>
[TD="bgcolor: #cacaca, align: center"]1[/TD]
[TD="align: right"]5570391[/TD]
[TD="align: right"]0135579[/TD]
[TD="align: right"]9755310[/TD]

[TD="bgcolor: #cacaca, align: center"]2[/TD]
[TD="align: right"]4513217[/TD]
[TD="align: right"]1123457[/TD]
[TD="align: right"]7543211[/TD]

[TD="bgcolor: #cacaca, align: center"]3[/TD]
[TD="align: right"]5373478[/TD]
[TD="align: right"]3345778[/TD]
[TD="align: right"]8775433[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]
[TD="align: right"]150630957[/TD]
[TD="align: right"]001355679[/TD]
[TD="align: right"]976553100[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]
[TD="align: right"]319910087[/TD]
[TD="align: right"]001137899[/TD]
[TD="align: right"]998731100[/TD]

</tbody>

Spreadsheet Formulas
CellFormula
B1{=REPT("0",LEN(A1)-LEN(SUBSTITUTE(A1,"0","")))&SUM(LARGE(1*(MID(A1,ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1))),1)),ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1))))*10^(ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1)))-1))}
C1{=""&SUM(SMALL(1*(MID(A1,ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1))),1)),ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1))))*10^(ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1)))-1))}

<tbody>
</tbody>
Formula Array:
Produce enclosing
{ } by entering
formula with CTRL+SHIFT+ENTER!

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4


Hello Istavan,

Thank you for your coding, can you please describe the function you used seperately.
Looking forward for your prompt response,

Best Regard,
Kamran
 
Upvote 0
Here is the regular formula solution

1] Ascending sort digits :
=TEXT(SUMPRODUCT(AGGREGATE(14,6,--MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1),ROW(INDIRECT("1:"&LEN(A2))))*10^(ROW(INDIRECT("1:"&LEN(A2)))-1)),REPT("0",LEN(A2)))

2] Descending sort digits :
=TEXT(SUMPRODUCT(AGGREGATE(15,6,--MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1),ROW(INDIRECT("1:"&LEN(A2))))*10^(ROW(INDIRECT("1:"&LEN(A2)))-1)),REPT("0",LEN(A2)))

Regards
Bosco

Thanks bosco,

Its working perfectly, can you please describe the function you used.

Best Regard,
Kamran
 
Upvote 0
Some more formula options assuming you have the CONCAT() function (Office 365)

1. If all your numbers are 7 digits like your samples.

Excel Workbook
ABC
1NumAscDesc
2557391413455799755431
3451321711234577543211
4537347833457788775433
5156395713556799765531
Sort 7 digits




2. If your numbers can vary in length.
These are array formulas so should be entered without the {} but confirmed with Ctrl+Shift+Enter, not just Enter. If confirmed correctly, Excel will insert the {}. The formulas can then be copied down.

Excel Workbook
ABC
1NumAscDesc
2557391420012345579975543210
3561156651
4203560235665320
5156395713556799765531
Sort any length
 
Upvote 0
.. and if you are interested, here is another UDF approach.

Code:
Function OrderDigits(s As String, Optional bDesc As Boolean) As String
  Dim a(0 To 9) As String, i As Long
  
  For i = 1 To Len(s)
    a(Mid(s, i, 1)) = a(Mid(s, i, 1)) & Mid(s, i, 1)
  Next i
  OrderDigits = Join(a, "")
  If bDesc Then OrderDigits = StrReverse(OrderDigits)
End Function

Excel Workbook
ABC
1NumAscDesc
2557391420012345579975543210
3561156651
4203560235665320
5156395713556799765531
Sort UDF
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top