I wanted to check if there was a way of encoding an integer into bytes that would be binary comparable.
Example: say I have integers -1 and 1
a) Write -1 to byte array b1[], write 1 to b2[]
b) b1[] should compare less than b2[]
If we write in big endian order I believe it works for positive integers but not negative ones.
I am trying to do this for a Hadoop custom raw key comparator…
Thanks
Ajai
This is in C#, but you should be able to do something similar to this in your language of choice, it’ll give you the correct sort order for positive as well as negative integers.
Note that you need to use an unsigned byte type to sort correctly.