#099 Think Ahead
Dealing with numbers when working with an text sort.
Q: Why the the leading zeros?
A: I'm Thinking ahead.
When working with numbers with text, adding leading zeros to a number keeps the alpha sort in correct order. In many programs the text sort reads the number from left to right as text. For example, 1 - 10 would probably sort fine, depending on the program. But, add the text 'cups of coffee' after the number and it will usually sort like this:
- 1 cup of coffee
- 10 cups of coffee
- 2 cups of coffee
- 3 cups of coffee
- 4 cups of coffee
- 5 cups of coffee
- 6 cups of coffee
- 7 cups of coffee
- 8 cups of coffee
- 9 cups of coffee
Adding adding leading zeros forces the sort to be correct.
- 01 cup of coffee
- 02 cups of coffee
- 03 cups of coffee
- 04 cups of coffee
- 05 cups of coffee
- 06 cups of coffee
- 07 cups of coffee
- 08 cups of coffee
- 09 cups of coffee
- 10 cups of coffee
This technique works especially well when naming files on your computer.
When I thought about this blog series I knew I'd have over 10 things to say, so I added a leading zero to 1 - 9. But when I started jotting down ideas it occurred to me that if, by chance, I write more than 99 I would have to go edit 90 posts. Yikes! So 3 digits it is.
Oh dear, now I have that Clutch song stuck in my head...
Facebook Comments