Two Stage Sorting by Formula Only

wdjohnson

New Member
Joined
May 24, 2016
Messages
12
Hi folks. Here is my scenario I'm trying to sort with using only formulas (older external program will not read .xlsm or .xlsx). I'll spare you the details of my own hackneyed attempt but let's just say I felt I was getting close with INDEX, MATCH, FIND and VLOOKUP but hit a wall.

Columns A and B: categories in order I need the data sorted by (non-alphabetic)
This list will be the only one customizable by the end user, typically 50+ entries per project and list would change/lengthen/shorten frequently from project to project:

DO = 001
RAY = 002
MI = 003
FA = 004
SO = 005
LA = 006
TI = 007

Column C: Non-sorted data imported from FileNameList
RAY01
RAY05
RAY03
DO01
FA02
TI05

Column D: a helper column of some kind I imagine, for example the first three digits are by finding the text in Column A and inserting corresponding number in column B, after decimal is the "rank" within each category:
002.1
002.3
002.2
001.1
004.1
007.1

Column E, output data sorted according to Column D

DO01
RAY01
RAY03
RAY05
FA02
TI05

Hopefully this makes sense. Maybe I need another helper column? Maybe I'm approaching it totally wrong? The variables being different lengths, skips in the ranking, duplicate rankings, my brute force methods are not cutting it. Any insight would be great!

Thanks in advance.
W. Johnson
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Does the imported data in column C always have two digit numbers at the end? Or might there be a RAY123
 
Last edited:
Upvote 0
Thanks for the reply. Yes there may be one two or three digits depending on the imported data. I have a test file with a few entries and the direction I was trying to go but I can't figure out how to upload it haha.
 
Upvote 0
When sorting, which comes first, RAY01 or RAY001?
Is RAY010 the same or different than RAY01
 
Last edited:
Upvote 0
The imported data could come in as so

XY12345_RAY99.pdf
XY12345_RAY100.pdf

And I'm using a MID function to chop off the prefix XY12345_ and suffix .pdf

The creater of the file should have named 99 as 099 but they didn't. Hopefully the function can bypass that anyway (or if not, throw a warning to go rename the file numbers)?
 
Upvote 0
Here is a solution that uses lots of helper columns

Unknown
ABCDEFGHIJK
1DO001RAY01RAY0020012.0011.001DODO.001DO001
2RAY002RAY05RAY0020052.0052.001RAYRAY.001RAY001
3MI003RAY03RAY0020032.0032.003RAYRAY.003RAY003
4FA004DO01DO0010011.0012.005RAYRAY.005RAY005
5SO005FA02FA0040024.0022.999RAYRAY.999RAY999
6LA006TI05TI0070057.0053.099MIMI.099MI099
7TI007RAY999RAY0029992.9994.002FAFA.002FA002
8MI99MI0030993.0997.005TITI.005TI005
9
Sheet1
Cell Formulas
RangeFormula
F1=RIGHT("000"&SUBSTITUTE($C1,D1,""),3)
G1=0+(E1&"."&F1)
H1=SMALL(G:G, ROWS($1:1))
I1=INDEX($A:$A,INT(H1),1)
J1=I1&TEXT(MOD(H1,1),".000")
K1=SUBSTITUTE(J1,".","")
D1{=LEFT(Sheet1!$C1, MIN((FIND({1,2,3,4,5,6,7,8,9,0}, Sheet1!$C1&"12345567890")))-1)}
E1{=VLOOKUP(D1,$A$1:$B$7,2,FALSE)}
Press CTRL+SHIFT+ENTER to enter array formulas.


Columns A and B are the letter to number chart
Column C is the imported data
Column D is the text part of the data
Column E converts that text to its matching number
Column F is the number part of column C
Column G combines those into a single number
Column H sorts column G
Column I is the text portion per column H
Column J combines column I with the decimal part of column H
And (finally) column K removes the decimal point.

That's a lot of columns. They can be combined. To avoid monster formulas, one can define two Names
Select a cell in row 1 and define the two names. (Note the absolute column/relative row referencing)
Name: LetterPart RefersTo: =LEFT(Sheet1!$C1,MIN((FIND({1,2,3,4,5,6,7,8,9,0}, Sheet1!$C1&"12345567890")))-1)
Name: NumberPart RefersTo: =RIGHT("000"&SUBSTITUTE(Sheet1!$C1,LetterPart,""),3)


Unknown
ABCDEF
1DO001RAY012.0011.001DO001
2RAY002RAY052.0052.001RAY001
3MI003RAY032.0032.003RAY003
4FA004DO011.0012.005RAY005
5SO005FA024.0022.999RAY999
6LA006TI057.0053.099MI099
7TI007RAY9992.9994.002FA002
8MI993.0997.005TI005
Sheet1
Cell Formulas
RangeFormula
D1=0+(VLOOKUP(LetterPart,$A$1:$B$7,2,FALSE)&"."&NumberPart)
E1=SMALL(D:D,ROWS($1:1))
F1=SUBSTITUTE(SUBSTITUTE(E1,INT(E1),INDEX($A$1:$B$7,INT(E1),1),1),".","")
Named Ranges
NameRefers ToCells
LetterPart=LEFT(Sheet1!$C1,MIN((FIND({1,2,3,4,5,6,7,8,9,0}, Sheet1!$C1&"12345567890")))-1)
NumberPart=RIGHT("000"&SUBSTITUTE(Sheet1!$C1,LetterPart,""),3)


Column D is the data converted to numbers
Column E is that sorted
Column F returns those number to text+number format.
 
Upvote 0
Thanks so much. Good call on the defined names up to Column D. From there I did a simple 1 thru X RANK in Column E. Lastly an INDEX,MATCH,ROWS to sort

Code:
=IFERROR(INDEX($C$1:$C$1000,MATCH(ROWS($E$1:E1),$E$1:$E$1000,0)),"")

Thanks for helping out a pure stranger!
 
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,574
Members
449,173
Latest member
Kon123

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