What Is Natural Sort?
You might need natural sorting when ordering filenames gives you this order:
Natural sorting is about considering the numeric value of group(s) of digits in a string instead of considering the digits as single characters. This way the filenames above are sorted this way:
Which feels more natural… Doesn’t it?
Download the demo project at https://github.com/zaatardigital/natural-sort and open the project in Xojo’s IDE. Select the ‘NaturalSortLib’ module, then copy & paste it into your project. You can close the demo project and save your own project.
NaturalSortLib doesn’t do the actual sorting. Instead it tells you which precedes the other in ascending order. If you want to compare the
bar strings, you just have to call the
NaturalSortLib.Compare() protected function like this:
Dim theResult As Integer = NaturalSort.Compare( foo, bar )
barare equals on a natural basis and it’s up to you to decide.
NaturalSortLib.Compare() is very easy to use in
ListBox.CompareRows() event. As easy as this:
Function CompareRows(row1 as Integer, row2 as Integer, column as Integer, ByRef result as Integer ) As Boolean result = NaturalSortLib.Compare( Me.Cell( row1, column ), Me.Cell( Row2,Column ) ) Return True End Function
See http://developer.xojo.com/listbox for more information about
To perform the natural comparison, strings are being splitted in differents parts either numerical or text parts. To speed up the splitting when a particular string is compared multiple times which is part of all sorting process, the split result is cached in a
To avoid having too much memory used by this cache, you should consider calling
NaturalSortLib.ClearNaturalSplitCache() each time a sort operation is complete.
Take a look at the source code and its comments to get more details about the implementation of the “natural” split and the caching.
If you find a bug or have a feature request feel free go to the project’s GitHub page at https://github.com/zaatardigital/natural-sort.
© Eric de La Rochette / June 2017