I just had the same issue with some formula that was trimming text. (ex.: parsing 1920x1080 to return only 1920 with TEXTBEFORE function).
It would always revert when I tried to put it as a number column and so it would only sort by text which meant 1200 came before 200. My solution was to do a useless math operation on it (SUM + 0) and now the cells and entire column is able to be formatted as a number again and sorted properly - format is retained.