Ask Reuben
Sort Comparison Function
How can I change the sort order used when I click on a column header?
How can I change sort order used with the array.sort method?
How can I sort like iTunes and exclude ‘The’ from the beginning of a word for sort comparison purposes?
How can I sort like Explorer where numbers may be simplified i.e. 1000 is 1K ?
How can I sort character data numerically i.e. 1,2,10, not 1,10,2?
When you look around other applications a question you should ask yourself is how would I implement that in a Genero application? One such question arises from programs like Windows Explorer, Mac OSX Finder. If you note the file size column, the size is simplified so that instead of saying 2000 or 1000000, it might say 2K, 1M. When you click on the column header, it still sorts correctly, that is 2K < 1M
You may also see something similar in the app you use for your music and movies. Is "The Beatles" sorted as if it begins with T or B? Is "The Lord of the Rings" sorted as if it began with T or L?
In a recent Genero maintenance release, functionality was added so that you can control the sort comparison function used.
This is in the form of two new methods.
Both of these methods use the concept of function references to allow you to pass the name of a function to a method. This function is then used to compare two values from the array and return a value that indicates the relationship between the two functions. The sort comparison function must have the following signature Examples of a sort comparison function might be Some things to note. Demo programs were added to $FGLDIR/demo/Sorting and can be found in the Demo program under New to 4.01 -> Enhanced Sorting. The screenshot below is from one of those demo programs and compares the sorting of clothing sizes, 3XS, XXS, XS, M, L, XL, XXL, 3XL. The default sort is alphabetical, whilst the user sort uses a sort comparison function to achieve the desired sort order.
FUNCTION (s1 STRING, s2 STRING) RETURNS INTEGER
and it is expected to return a negative value to indicate that s1 should appear before s2, a positive value to indicate that s1 should appear after s2.-- A sort that ignores The if the sort column begins The ...
FUNCTION itunes_sort(s1 STRING, s2 STRING) RETURNS INTEGER
IF s1.subString(1, 4) = "The " THEN
LET s1 = s1.subString(5, s1.getLength())
END IF
IF s2.subString(1, 4) = "The " THEN
LET s2 = s2.subString(5, s2.getLength())
END IF
RETURN util.Strings.collate(s1,s2)
END FUNCTION
-- Sort based on length of sort value
FUNCTION length_sort(s1 STRING, s2 STRING) RETURNS INTEGER
RETURN s1.getLength() - s2.getLength()
END FUNCTION