Google-sheets – Applying a custom sort to Google Sheets string column

google sheetsgoogle-sheets-queryregexregexextractsorting

I am trying to sort a table of rows by their first column that is comprised of strings with this format (capital letters followed by a dash followed by numbers).

Using the default string sort I get something like this:

BB-1
BB-11
BB-12
BB-2
BB-3
BL-1
BL-14
BL-15
MAN-4
MAN-45
MAN-5

How can I sort it first by the char prefix followed by the integer suffix so my result is as follows:

BB-1
BB-2
BB-3
BB-11
BB-12
BL-1
BL-14
BL-15
MAN-4
MAN-5
MAN-45

I've attempted to do this using a formula with some regular expressions, but it doesn't seem to do the trick:

=sort(A2:A,REGEXEXTRACT(A2:A,"^\w+"),true, REGEXEXTRACT(A2:A, "\d+$"), true)

Best Answer

This formula did the trick:

=sort(A2:A,REGEXEXTRACT(A2:A,"^\w+"),true, VALUE(REGEXEXTRACT(A2:A, "\d+$")), true)

My first attempt was missing the string to value conversion.