• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Sort digits without using "Sort"

fred3

Member
I have a large number 10-digit number in rows in both contiguous form and separated into 1-digit per column.

I want to reorder the digits in the rows so that the most-frequent digit is first, the next-most frequent is 2nd, etc.

So:
2 5 2 4 4 2 6 6 1 6
Would translate to:
2 2 2 6 6 6 4 4 1 5

And, I want to make it dynamic and not use VBA.

How might that be done?
 
Hi, @fred3!

Check this formula in attach file:
[L1] : =INDEX($A1:$J1,10-RIGHT(AGGREGATE(14,,10000*COUNTIF($A1:$J1,$A1:$J1)+100*(10-MATCH($A1:$J1,$A1:$J1,))+COLUMN($J1)-COLUMN($A1:$J1),COLUMNS($L1:L1)),2))

And drag it to right. Blessings!
 

Attachments

  • SortbyFrequency.xlsx
    9 KB · Views: 14
Hi, @fred3!

If this is not essential, the big digit will be the first
6 6 6 2 2 2 4 4 5 1


L1 =LEFT(AGGREGATE(14,6,--REPT($A$1:$J$1,COUNTIF($A1:$J1,$A1:$J1)),COLUMN(A1)),1)+0

And drag it to right.

If you want a different order,
1 5 4 4 2 2 2 6 6 6

L1=LEFT(AGGREGATE(15,6,--REPT($A$1:$J$1,COUNTIF($A1:$J1,$A1:$J1)),COLUMN(A1)),1)+0

And drag it to right.


David
 
Last edited:
I don't get:
Code:
COUNTIF($A1:$J1,$A1:$J1)

I'd not seen AGGREGATE before. Very interesting and useful I should think.

I'm having trouble inserting a column at col A; or, in general, moving the code around on the sheet.
If I insert col A, then the last output point goes to #NUM! and translating the formula seems to not work as expected.
Here is the formula translated one column by inserting a column at col A:
Code:
=LEFT(AGGREGATE(14,6,--REPT($B$1:$K$1,COUNTIF($B$1:$K$1,$B$1:$K$1)),COLUMN(B1)),1)+0
 
Last edited:
Hi, @fred3!

=LEFT(AGGREGATE(14,6,--REPT($A1:$J1,COUNTIF($A1:$J1,$A1:$J1)),COLUMN(A$1)),1)+0

Now, drag right and down.

COLUMN(A$1)>That means the first result
COLUMN(B$1)>This means second result
And so on, to be dynamic .

David
 
Last edited:
That appears to be the expression that I started with and it works. But I need to copy and paste (or type) into a shifted location on another spreadsheet.
All I did was insert a column A and then it broke!
The result is attached.
There's something I'm not getting quite yet.
 

Attachments

  • Probabilities.xlsx
    307.7 KB · Views: 5
Back
Top