Is there a well-known algorithm for converting an arbitrary number (either float or integer) into a string representation in such a way that the sort order is maintained?
To illustrate what I mean: if my numbers would be limited to be integers in the range [0,9999]
, it's very simple: I could just pad all numbers to have four digits:
1000
200
30
4
=>
0004
0030
0200
1000
But how do I do this for arbitrary numbers, including integers, floats and negatives?
My usecase is that I want to be able to store numbers as string in a database, and still be able to sort on them.