SORT
NAME
sort - Sorts an array.
SYNOPSIS
sort([sort:integer,fname:string] array)
DESCRIPTION
This function sorts an array. By default sort will look at the first element to guest the sorting type.
You can specify the sort type to be 0 for integer, 1 for string, 2 for your sort function and 3 for float.
A sort function receive two element and you must compare them.
PARAMETERS
array (array) : the array for sorting.
sort (int - optional): 0 for integer, 1 for string, 2 for your sort function and 3 for float.
fname: required if sort is set to 2. It's a string value referring to a defined custom sort function/algerithm. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. This is specially useful when sorting an associative array.
RETURN
Returns the sorted array.
EXAMPLES
sort([1.5,5,2,98,32,7,2,5]);
// [1.5,2,2,5,5,7,32,98]
sort([1,5,2,98,32,7,2,5]);
// [1,2,2,5,5,7,32,98]
sort(sort:0,[1,5,2,98,32,7,2,5]);
// [1,2,2,5,5,7,32,98]
sort(sort:1,['gt','ab', 'ba', 'cd']);
// ["ab","ba","cd","gt"]
function f(a,b)
return b - a;//this will sort the array in descending order because when b > a, it returns positive integer; when b = a, it returns 0; when b < a, it returns negative integer.
endf
sort(sort:2,fname:"f",[1,5,2,98,32,7,2,5]);
// [98,32,7,5,5,2,2,1]
// When sorting an array of contexts, use operator `cmp` or `<=>`
// Sort for NUMBER, use `<=>` operator, ex: ASC
aa = [
{"name":"xp", "number": 2},
{"name":"dd", "number": 10},
{"name":"gg", "number": 1},
{"name":"to", "number": 100}
];
function f1(a,b)
return a.number <=> b.number;
endf
sort(sort:2,fname:"f1",aa);
// [{"name":"gg","number":1},{"name":"xp","number":2},{"number":10,"name":"dd"},{"number":100,"name":"to"}]
// Sort for NUMBER, use `<=>` operator, ex: DESC
aa = [
{"name":"xp", "number": 2},
{"name":"dd", "number": 10},
{"name":"gg", "number": 1},
{"name":"to", "number": 100}
];
function f1(a,b)
return b.number <=> a.number;
endf
sort(sort:2,fname:"f1",aa);
// [{"number":100,"name":"to"},{"name":"dd","number":10},{"number":2,"name":"xp"},{"number":1,"name":"gg"}]
// Sort for STRING, use `cmp` operator, ex: ASC
aa = [
{"name":"xp", "code": "ab"},
{"name":"dd", "code": "bb"},
{"name":"gg", "code": "cb"},
{"name":"to", "code": "db"}
];
function f1(a,b)
return a.code cmp b.code;
endf
sort(sort:2,fname:"f1",aa);
// [{"code":"ab","name":"xp"},{"code":"bb","name":"dd"},{"code":"cb","name":"gg"},{"code":"db","name":"to"}]
// Sort for STRING, use `cmp` operator, ex: DESC
aa = [
{"name":"xp", "code": "ab"},
{"name":"dd", "code": "bb"},
{"name":"gg", "code": "cb"},
{"name":"to", "code": "db"}
];
function f1(a,b)
return b.code cmp a.code; // For DESC, inverse the position of `a` and `b`
endf
sort(sort:2,fname:"f1",aa);
// [{"code":"db","name":"to"},{"code":"cb","name":"gg"},{"code":"bb","name":"dd"},{"code":"ab","name":"xp"}]
SEE ALSO
{{ include("includes/array.sn") }}
AUTHOR
Written by Pierre Laplante, <laplante@sednove.com>
MODIFICATIONS
1.0 2014-09-09 21:24:14 laplante@sednove.com
Edit