built-in functions should work as sort comparators

Description

See http://blog.adamcameron.me/2017/04/cfml-lucee-13-v-4-14-v-6-coldfusion-we.html

See this test case:

component extends="testbox.system.BaseSpec" { function run() { describe("Other sort tests", function(){ it("is a baseline showing using BIFs as a callback", function(){ var testString = "AbCd"; var applyTo = function(object, operation){ return operation(object); }; var result = applyTo(testString, ucase); expect(result).toBeWithCase("ABCD"); }); describe("using arrays", function(){ it("can use a function expression calling compareNoCase as a string comparator when sorting", function(){ var arrayToSort = ["d","C","b","A"]; arrayToSort.sort(function(e1,e2){ return compareNoCase(e1, e2); }); expect(arrayToSort).toBe(["A","b","C","d"]); }); it("can use the compareNoCase BIF as a string comparator when sorting", function(){ var arrayToSort = ["d","C","b","A"]; arrayToSort.sort(compareNoCase); expect(arrayToSort).toBe(["A","b","C","d"]); }); }); describe("using lists", function(){ it("can use a function expression calling compareNoCase as a string comparator when sorting", function(){ var listToSort = "d,C,b,A"; var sortedList = listToSort.listSort(function(e1,e2){ return compareNoCase(e1, e2); }); expect(sortedList).toBe("A,b,C,d"); expect(listToSort).toBe("d,C,b,A"); }); it("can use the compareNoCase BIF as a string comparator when sorting", function(){ var listToSort = "d,C,b,A"; var sortedList = listToSort.listSort(compareNoCase); expect(sortedList).toBe("A,b,C,d"); expect(listToSort).toBe("d,C,b,A"); }); }); }); } }

Specifically:

arrayToSort.sort(compareNoCase);

And:

var sortedList = listToSort.listSort(compareNoCase);

Activity

Show:

Michael Offner 30 April 2024 at 18:23

interesting idea, but not possible for one simple reason, UDF are objects in CFML, but BIFS are not, in short you can do

function test() {} susi=test; susi();

because UDFs are objects, but you cannot do

susi=arrayLen; susi([]);

but you can use compareNoCase inside a closure as you already suggested.

Pothys - MitrahSoft 8 May 2017 at 10:28

I've added test case for this ticket.If we use build in function as a sorting comparators, It doesn't support in lucee. In ACf 16, array sort using compareNocase() as sort comparator, it doesn't throw any error, but no changes in result array. In list sort function using compareNocase() as sort comparator, ACf throw error "Parameter validation error for the listsort function".

Pull Request: https://github.com/lucee/Lucee/pull/230

Fixed

Details

Assignee

Reporter

New Issue warning screen

Before you create a new Issue, please post to the mailing list first https://dev.lucee.org

Once the issue has been verified, one of the Lucee team will ask you to file an issue

Affects versions

Priority

Created 2 May 2017 at 08:59
Updated 30 April 2024 at 18:23
Resolved 30 April 2024 at 18:23

Flag notifications