Sorting in Numbers

I'm trying to sort a column in Numbers in ascending order. The column has a code the is letter letter number number number, so for example


HG123


It would be read as HG one hundred and twenty three.


For some reason (I'm sure it's super obvious but I can't figure it out) it is sorting only based on the first number in the sequence. So HG110 is next to HG11 and HG99 is at the very end of the column. I'm guessing it has something to do with how I have the data in the column formatted? I don't know I'm new to Numbers and totally at a lost for something that seems like it should be relatively straight forward.


But ideally I would like it to read the entire number that comes after the letter code and sort that sequentially, so that HG100 comes after HG99, not HG100 up at the top by HG10.

Posted on Dec 13, 2023 5:54 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 13, 2023 7:13 PM

Hi MB_1014,


Data in Column B are Text strings and will sort alphabetically. Separate the numerical part into column C like this:



Formula in C2 is VALUE(TEXTAFTER(B2,C$1,1))

TEXTAFTER pulls all characters in B2 after HG (or whatever is in the Header Cell C1).

VALUE converts those characters to a number.

Formula in D2 is C$1&C2 to recombine the HG with the number part.


Sort the table by Column C Ascending



If Column D is now what you want, you can:

  • Save as Template for future use, or
  • Copy Column D and Menu > Edit > Paste Formula Results to "fix" the results. Then delete Columns B and C.


Regards,

Ian.


Edit: On rereading, I now realise that Column D is not needed. Column B is now sorted 🤔.

Ian.

5 replies
Question marked as Top-ranking reply

Dec 13, 2023 7:13 PM in response to MB_1014

Hi MB_1014,


Data in Column B are Text strings and will sort alphabetically. Separate the numerical part into column C like this:



Formula in C2 is VALUE(TEXTAFTER(B2,C$1,1))

TEXTAFTER pulls all characters in B2 after HG (or whatever is in the Header Cell C1).

VALUE converts those characters to a number.

Formula in D2 is C$1&C2 to recombine the HG with the number part.


Sort the table by Column C Ascending



If Column D is now what you want, you can:

  • Save as Template for future use, or
  • Copy Column D and Menu > Edit > Paste Formula Results to "fix" the results. Then delete Columns B and C.


Regards,

Ian.


Edit: On rereading, I now realise that Column D is not needed. Column B is now sorted 🤔.

Ian.

Dec 19, 2023 6:38 AM in response to MB_1014

In cases where I want a list of text+index numbers to sort correctly, whether in Numbers or in a file structure or whatever app, I pad the numeric part so all have the same number of digits. I am assuming you want all the HG's together then HH's, etc, as HG100, HG101, HH100, HH101 and not sorted as HG100, HH100, HG101, HH101


If you are using three digits max then it would be HG011 not HG11

Dec 18, 2023 8:12 PM in response to MB_1014

Here's another approach using recent features in Numbers. If there are more alpha codes in front than just HG then this is a more compact solution (needs just one extra column), but comes at the cost of a more complex formula.




This uses regular expressions to extract the alpha [\D] and numeric [\d] portions, then REPT to pad the numeric portion with leading zero(s) if needed, and then puts the two back together again into a string that will sort in the order described.


If the numeric portion has more than 3 digits then simply increase the 3 in the formula.


=REGEX.EXTRACT($B2,"[\D]+")&REPT(0,3−LEN(REGEX.EXTRACT($B2,"[\d]+")))&REGEX.EXTRACT($B2,"[\d]+")


Replace the , in the formula with ; for regions that use , as the decimal separator.


SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Sorting in Numbers

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.