Sorting strings by respecting natural order for numbers - 6 octobre 2012
A function that is useful is a comparator of strings (for sorting purpose, in general) that respect the natural order of numbers.
So when we have a list of strings, like file names, for examples, with numbers in them, it is better to respect the numerical order when strings are equal.
A common case is numbered names: a trivial, lexicographical order would be:
foo-1
foo-10
foo-11
foo-2
...
foo-9
where one expects:
foo-1
foo-2
...
foo-9
foo-10
foo-11
I show here how to do such comparator.
Continuer à lire "Sorting strings by respecting natural order for numbers"
Is this string a number? - 6 décembre 2010
A blog post (Double Parsing Regexp) caught my attention. The base problem is to check if a string is a valid number, without necessarily needing the real value. Or, perhaps, to skip the conversion if we are sure it will fail, as Double.valueOf throws an exception in this case, and the try/catch mechanism is known to be slow.
The numbers given were intriguing, and I wondered if the given regular expression could be optimized. Somehow, it was a great opportunity to use Caliper a library to do correctly micro-benchmarks.
Continuer à lire "Is this string a number?"