Tips and tricks: Sorting of texts containing figures


Today tip is about the way of sorting numeric values in a non-numeric format. In particular, we will take a look at an example of sorting component versions - e.g. "". Values input in such a way are not sorted like numerals but like texts. In case, that some of the figures contains two or more characters, it will be sorted in a different way than we would expect:


The value 12 is sorted behind 1. However, we would expect such an order:


ObjectGears offers a solution in form of a function that creates a regular integer out of these "dotted" figures. The function will split the text according to the separator, it will add zeros to the left of each figure according to the parameter maxNumberLength and merges the strings in a single integer.

OG.TextUtils.CreateDottedText() and OG.TextUtils.CreateDottedText(minNumbers, maxNumbers, maxNumberLength)



var dt = OG.TextUtils.CreateDottedText(4, 4, 2);

var l = dt.ParseLong("");



l = 1120401;


Now, you just need to create another column in the class (type integer) and create a rule (type Before saving a record), in which you will read text of the version, create a numeric version and save it into this new column.

After that you will define sorting in the class according to this new column.

Take a look also at the documentation.

This website is using cookies files to provide services and analyse visits. You agree with that by using this website.     Further information