0

Help with sorting on 4 columns

Using the method suggested elsewhere in the forum I created a sorting number/code to sort 4 columns. Its of the form Value1"."Value2"."Value3"."Value4.  This results in a field that gives me a result in the form 1.1.1.1

This works for sorting when the values are single digit. However, if there 12 records for 1.1.1.X then the sort results in 

1.1.1.1, 1.1.1.10, 1.1.1.11, 1.1.1.12, 1.1.1.2

I can see how this might come about but I can't see a way around it. I can't force the final digit to go to "01".

Perhaps this isn't possible but I'd appreciate any comments on

a) A way around my immediate problem

OR

b) an alternative solution

 

Thanks

8 replies

null
    • Sean
    • 3 yrs ago
    • Reported - view

    When you concatenate numbers, they are converted to text and sorted as text. I would multiply each value by an appropriate weight and then sum the values.

    • Alain_Fontaine
    • 3 yrs ago
    • Reported - view

    If the numbers are integers, you can also format them with an appropriate number of leading zeroes:

    format(Value1,"000000000") + format(Value2,"000000000") + format(Value3,"000000000") + format(Value4,"000000000")

    • Dorich
    • 3 yrs ago
    • Reported - view

    @Sean:

    Thanks that got me to a working solution.

    • Dorich
    • 3 yrs ago
    • Reported - view

    @ Alain Fontaine

    Thanks, I didn't realize I could do that.

    • Sean
    • 3 yrs ago
    • Reported - view

    The problem with using the format() function is the same as with the text() or concat() functions. Sorting will be based on the numbers' ASCII values and not their numeric values.

    • Alain_Fontaine
    • 3 yrs ago
    • Reported - view

    When numbers formatted as text are sorted, the sorting is indeed performed on the numerical values of the ASCII (or Unicode) codes of the characters representing the digits. Is it a problem? It depends... First, the numerical values of the ASCII codes of the characters representing the digits happen to be ordered in the same way as the numerical values of the digits themselves. So far so good. The problem arises when sorting numbers whose length, when represented as strings showing their decimal value, is different. The essence of the problem is that a string comparison is performed by aligning the strings on the left, and then proceeding from left to right. So, if the number 2 is represented by the string "2", and the number 10 by the string "10", performing a string comparison, left aligned, first compares "2" with "1", and concludes that "2" is greater than "10". Now, if one formats the numbers with leading zeroes, the number representations as strings become, for example with the format "000", "002" and "010". One can easily verify that the digits are now correctly aligned, and that a string comparison gives the correct result.

    The two techniques presented in this thread, by multiplication or by formatting, work equally well for a small number of small numbers. If many numbers, where each one can take a large value, are involved, the multipliers can rapidly need to take large values. It has been recently demonstrated by Jacques Tur that the arithmetic implementation in Ninox can only handle about 15 decimal places, which sets a limit on the usable multipliers. With the formatting technique, the limit is the size of the string that can be handled.

    • Sean
    • 3 yrs ago
    • Reported - view

    😎 I'll keep that in mind the next time I do a 16+ column sort.

    • Dorich
    • 3 yrs ago
    • Reported - view

    @ Alain Fontaine

    thanks for the additional explanation. For novices like myself its very helpful to get some understanding of the NINOX process work.

Content aside

  • 3 yrs agoLast active
  • 8Replies
  • 514Views