Forum Discussion

ECD333285's avatar
ECD333285
Copper Contributor
Jul 04, 2024
Solved

Sorting on multiples in same column

Hi there - Cannot figure this out. I have a list of items in a column as follows: 5" White 5" Red 5" Blue 11" White 11" Red 11" Blue 17" White 17" Red 17" Blue There are several more sizes ...
  • djclements's avatar
    djclements
    Jul 10, 2024

    ECD333285 No worries. The original formula sorted the data alphanumerically, treating the sizes as text (anything that begins with a "1" appears first, then "2" and so on. To sort them in a specified order, you can create an array of sizes in the order of your choosing, then use the XMATCH method again to generate the size_ID. For example, the formula for Excel 2021 could be modified as follows:

     

    =LET(
        data, FILTER(A2:A1000, A2:A1000<>""),
        num, SEARCH(" ", data),
        color, RIGHT(data, LEN(data) - num),
        color_ID, XMATCH(color, UNIQUE(color)),
        size, --LEFT(data, num - 2),
        size_ID, XMATCH(size, {11;17;5;24;36}),
        SORTBY(data, color_ID, 1, size_ID, 1)
    )

     

    Also, if you run into the same problem with the colors not sorting in the preferred order, you could replace UNIQUE(color) with an array of colors in the order of your choosing. For example:

     

    =LET(
        data, FILTER(A2:A1000, A2:A1000<>""),
        num, SEARCH(" ", data),
        color, RIGHT(data, LEN(data) - num),
        color_ID, XMATCH(color, {"White";"Red";"Blue";"Yellow"}),
        size, --LEFT(data, num - 2),
        size_ID, XMATCH(size, {11;17;5;24;36}),
        SORTBY(data, color_ID, 1, size_ID, 1)
    )

     

    As an aside, I've defined variables for each step in the examples shown above so they're easy to understand and follow, but the same formula(s) can also be written with fewer steps:

     

    =LET(
        data, FILTER(A2:A1000, A2:A1000<>""),
        num, SEARCH(" ", data),
        SORTBY(
            data,
            XMATCH(RIGHT(data, LEN(data) - num), {"White";"Red";"Blue";"Yellow"}), 1,
            XMATCH(--LEFT(data, num - 2), {11;17;5;24;36}), 1
        )
    )

     

    I hope that works as expected. Cheers!

Resources