9

How can I sort a table in sheet 1 like

A B C D E
3 7 3 6 5

into another table in sheet 2

A C E D B
3 3 5 6 7

by using function only?

3
  • do you also need the column letters from the sorting or was this only for clarification? Commented Feb 21, 2013 at 18:18
  • @PeterAlbert I suppose letters as well - otherwise I could submit an answer long ago)) Commented Feb 21, 2013 at 18:25
  • @PeterAlbert i also need those letters Commented Feb 21, 2013 at 19:08

5 Answers 5

11

One really easy way to do it would be to just have a rank index and then use HLOOKUP to find the corresponding values:

=RANK(A4,$A$4:$E$4,1)

Example 1

=IF(COUNTIF($A$1:A$1,A1)>1,RANK(A4,$A$4:$E$4,1)+COUNTIF($A$1:A$1,A1)-1,RANK(A4,$A$4:$E$4,1))

Example 2

=HLOOKUP(COLUMN(),$A$2:$E$4,2,FALSE)

Example 3

=HLOOKUP(COLUMN(),$A$2:$E$4,3,FALSE)

Example 4

Sign up to request clarification or add additional context in comments.

4 Comments

I'd upvote that if you provided formulas in text. Also, what the difference between screens 3 and 4?
@PeterL. Give a man a fish and you feed him for a day. Teach a man to fish, and you feed him for a lifetime - or at least until some large fishing conglomerate expands into his village.
@Stepan1010: Nice solution with the additional countif and flexible relative/absolute references! only small improvement: in the second formula, replace RANK(A4,$A$4:$E$4,1) with A1, as you already calculated it above.
Couldn't you do the COUNTIF directly on the input numbers (line 4 rather than line 1)? Then you wouldn't need the first helper line (except for didactic purposes)...
11

Okay, here's the "one formula does it all" solution without additional temporary columns:

enter image description here

Formula in A6:

=INDEX($A$2:$E$2,MATCH(SMALL($A$3:$E$3+COLUMN($A$3:$E$3)/100000000,COLUMN()),$A$3:$E$3+COLUMN($A$3:$E$3)/100000000,0))

Enter it as an array formula, i.e. press Ctrl-Shift-Enter. Then copy it to the adjacent columns.

To get also the number, use this formula in A7 (again as array formula):

=ROUND(SMALL($A$3:$E$3+COLUMN($A$3:$E$3)/100000000,COLUMN()),6)

Both formulas are a bit bloated as they have to also handle the potential duplicates. The solution is to simply add a very tiny fraction of the column before applying the sorting (SMALL function) - and then to remove it again...

4 Comments

My fair upvote and still only 2 points to take 3k! that's worth +100. simply brilliant!
@PeterL.: thanks. it's actually the same formula as the solution before - only there the "getting the column letter" made it more complicated...
This trick with /10000000 is just a masterpiece) I'm crazy about such elegant solutions! Hell I'm still so ugly yet..)))))
I say, beautiful solution.
1

I think an easier solution is to use the "Large" function: excel screenshot

The Column functions are just an easy way to count from 5 down to 1, but a helper column may be even easier.

You can have a similar answer using the "Small" function as well.

Comments

1

Although I have used the "add a small number technique", I think this is the most elegant for a helper row/column:

=RANK(B4,$B$4:$F$4,0) + COUNTIF($B$4:$F$4,B4)-1

(copy across the row column) RANK gets you close and the COUNTIF portion handles duplicates by counting the number of duplicates of the cell to that point. Since there is always match (itself) you subtract 1 for the final rank. This "sorts" ties in the order that they appear. Note that an empty cell will generate #N/A and character data as #Values.

Comments

0

It's doable! :-)

Here is the sample file.

Explanation

I assume that your data is in row 1 of Sheet1 and that you want the data sorted starting in column 1 of another sheet - if not, adjust accordingly.

Place this formula in column 1 of the target sheet, say in row 2:

=ROUND(SMALL(Sheet1!$A$1:$E$1+COLUMN(Sheet1!$A$1:$E$1)/100000000,COLUMN()),6)

You need to enter the formula as an array formula, i.e. press Ctrl-Shift-Enter.

In case you place it in another column than column one, you need to replace COLUMN() with COLUMN()-COLUMN($A$2)+1 where $A$2must be a reference to the cell itself.

This formula will return you the lowest number in your range - if you copy it the next 4 columns, you'll get your order list of numbers.

To translate this back to the column number, we need to perform 2 steps:

  1. Figure out the column number:
    This can be done with with this formula:
    =MATCH(SMALL(Sheet1!$A$1:$E$1+COLUMN(Sheet1!$A$1:$E$1)/100000000,COLUMN()),Sheet1!$A$1:$E$1+COLUMN(Sheet1!$A$1:$E$1)/100000000,0)
    
    - again, to be entered as array formula. If the source data does not start in column A, you need to add +COLUMN(Sheet1!$A$1)-1 where $A$1 needs to be replaced with the leftmost cell of the source data.
  2. Convert the column number to a letter:
    Assuming your column number from step 1 is in cell A6, this formula will do the job:
=LEFT(ADDRESS(1,A6,2),SEARCH("$",ADDRESS(1,A6,2))-1)

Of course, you can also combine step 1&2, this will result in this megaformula:

=LEFT(
    ADDRESS(
        1,
        MATCH(
            SMALL(Sheet1!$A$1:$E$1+COLUMN(Sheet1!$A$1:$E$1)/100000000,COLUMN()),
            Sheet1!$A$1:$E$1+COLUMN(Sheet1!$A$1:$E$1)/100000000,
            0),
        2),
    SEARCH(
        "$",
         ADDRESS(
             1,
             MATCH(
                 SMALL(Sheet1!$A$1:$E$1+COLUMN(Sheet1!$A$1:$E$1)/100000000,COLUMN()),
                 Sheet1!$A$1:$E$1+COLUMN(Sheet1!$A$1:$E$1)/100000000,0),
             2)
     )-1
)
Again, to be entered as array formula.

7 Comments

Peter, I took a look at your sample, but I suppose the idea of the request is that ANY text may be on place of ABCDE. Will your solution handle it?
@PeterL.: my understanding of the question is that it is about sorting the numbers, the letter are only the column letters. so it sorts the numbers and shows in which column the original data was located.
well, assuming the input (OP addresses it as table) - I don't think so. Otherwise dynamic sort of 1D num array is done with "trivial" array formula I could post at a glance)
the solution can be easily adjusted - only need to replace the "convert column number" to an INDEX formula. Will adjust the answer. However, what makes the sorting a bit complicated is the fact that multiple occurrence of the same value are possible. so the array formula is not that easy, but doable (as shown in the solution) :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.