sortingexcel-formulacell

Sorting numbers within a cell without omitting the 0


I would like to sort numbers from smallest to largest within a single cell. The current cell content is 1, 4, 0, 2, -9, -10 and should be -10, -9, 0, 1, 2, 4

I managed to sort it, however, the 0 is omitted in the output which is an issue. How can I make sure the entire content is sorted?

For reference, this is the current formula I am using:

=TEXTJOIN(", ",1,IFERROR(1/(1/SMALL(FILTERXML("<a><b>"&SUBSTITUTE(A5,",","</b><b>")&"</b></a>","//b"),ROW($1:$99))),""))

Solution

  • You could try using the TEXTSPLIT() function here:

    enter image description here


    • Formula used in cell B1

    =TEXTJOIN(", ",1,SORT(--TEXTSPLIT(A1,,", ")))
    

    • Or,

    =TEXTJOIN(", ",1,IFERROR(SMALL(FILTERXML("<m><b>"&SUBSTITUTE(A1,",","</b><b>")&"</b></m>","//b"),ROW(1:99)),""))
    

    • If you have access to LET() then and not TEXTSPLIT():

    =LET(
         a, 1/(1/SMALL(FILTERXML("<m><b>"&SUBSTITUTE(A1,",","</b><b>")&"</b></m>","//b"),ROW(1:99))),
         TEXTJOIN(", ",1,IFERROR(IFNA(IF(ERROR.TYPE(a)=2,0,a),a),"")))
    

    • Or,

    =TEXTJOIN(", ",1,SUBSTITUTE(IFERROR(1/(1/SMALL(--(FILTERXML("<a><b>"&SUBSTITUTE(A1,",","</b><b>")&"</b></a>","//b")&".1"),ROW($1:$99))),""),".1",))