Natural Sort (Part 2 of 3)

Before comparing strings we need to identify the text and the numeric parts in the string. The algorithm is very simple: iterate each character of the string, if we find a character that is not a digit, start a Characters chunk, if it’s a digit, create a new Number chunk. Process the next character. If it’s of the same kind than the previous one, then store it in a buffer and continue. If it’s not of the same type, then store the current buffer as a chunk part alongside it’s type, then create a new buffer and set the correct chunk type for it. Repeat the process until the end of the string.

Natural Sort (Part 1 of 3)

Sorting multiple strings means comparing them 2 by 2 and decide which precedes the other. Standard comparison algorithm compare the strings character by character based on their numerical value defined by the string encoding until they find a difference. Then the precedence is defined by the lowest numerical value coming first. Unfortunately, this has a side effect when it comes to sorting numeric values.

Truncating a Double

Xojo®‘s API provides us with three ways to convert a decimal number to a whole number: Round(), Floor() and Ceil(). But if you only need to truncate a decimal number to its integer part, there is no out-of-the-box method. So let’s implement our own truncate method.

Upside Down Arrays

Xojo® provides us with some interesting methods to manipulate arrays: Array, Join, Split, Append, IndexOf, Insert, Pop, Redim, Remove, Shuffle, Sort, SortWith, etc… You’ll find an exhaustive list in the Language Array category in Xojo®‘s Wiki. What you may notice is the lack of a method to reverse the order of the elements in the array. But this kind of manipulation shouldn’t be hard to code in Xojo®.

Don’t Break On Every Exception

When I’m working on a project in the IDE, I always leave the ‘Break On Exceptions’ menu item checked. I’m the kind of programmer who’s afraid of missing a special and random exception condition when running a project in the debugger. I just can’t help it, even if it proved useful only once or twice since I’m using Xojo® ( it was named REALbasic 2.1 at the time ). The counterpart is that each time an exception is raised, the debugger kicks in. Even if this exception condition is expected and properly handled. Fortunately, there is a pragma that can help.