Skip to content

Commit 21aeeba

Browse files
authored
add query sort closure examples (#1030)
* add closure examples
1 parent 5c00f62 commit 21aeeba

File tree

10 files changed

+61
-47
lines changed

10 files changed

+61
-47
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
if true it does a locale sensitive sorting.
1+
if true, use locale sensitive sorting.

docs/03.reference/01.functions/arraysort/_arguments/sorttype_or_closure.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ a string must be one of the following values:
77
- "textnocase": sorts text alphabetically, without regard to case (case insensitive)
88

99
if you define a closure/function, the closure/function must accept 2 parameters of any type and return:
10-
\-1, if first parameter is "smaller" than second parameter
11-
0, if first parameter is equal to second parameter
12-
1, first parameter is "bigger" than second parameter
10+
11+
- \-1, if first parameter is "smaller" than second parameter
12+
- 0, if first parameter is equal to second parameter
13+
- 1, first parameter is "bigger" than second parameter
14+
15+
`function (any e1, any e2) { return -1/0/1;});`
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
a list of names, a single column name or a function used as comparator.
1+
- a list of names
2+
- a single column name
3+
- or a function used as comparator with the following constructor
4+
5+
`function(struct row1, struct row2){ return true / false;}`.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
a list of directions or a single direction definition (asc,desc), the list must have the same length as the columnName list
1+
A list of directions or a single direction definition (asc, desc),
2+
3+
The list must have the same length as the columnName list.
4+
5+
Only used when the second argument defines a list of column names.
Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
11
```luceescript+trycf
22
people = QueryNew( "name,dob,age", "varchar,date,int", [
3-
[ "Susi", CreateDate( 1970, 1, 1 ), 0 ],
4-
[ "Urs" , CreateDate( 1995, 1, 1 ), 0 ],
5-
[ "Fred", CreateDate( 1960, 1, 1 ), 0 ],
6-
[ "Jim" , CreateDate( 1988, 1, 1 ), 0 ]
3+
[ "Susi", CreateDate( 1970, 1, 1 ), 70 ],
4+
[ "Urs" , CreateDate( 1995, 1, 1 ), 40 ],
5+
[ "Fred", CreateDate( 1960, 1, 1 ), 50 ],
6+
[ "Jim" , CreateDate( 1988, 1, 1 ), 30 ]
77
]);
88
9-
Dump( var=people, label="people - origional query" );
9+
Dump( var=people, label="people - original query" );
1010
11-
/* Output:
12-
13-
| name | dob | age |
14-
------------------------------------
15-
| Susi | 1970-01-01 00:00:00 | 0 |
16-
| Urs | 1995-01-01 00:00:00 | 0 |
17-
| Fred | 1960-01-01 00:00:00 | 0 |
18-
| Jim | 1988-01-01 00:00:00 | 0 |
19-
*/
20-
21-
people.sort('name', 'asc');
11+
QuerySort(people, 'name', 'asc');
2212
dump(var=people, label='people - sorted by name');
2313
24-
/* Output:
14+
QuerySort(people, function(row1, row2){
15+
return compare(arguments.row1.age, arguments.row2.age);
16+
});
17+
dump(var=people, label='people - sorted by age');
2518
26-
| name | dob | age |
27-
------------------------------------
28-
| Fred | 1960-01-01 00:00:00 | 0 |
29-
| Jim | 1988-01-01 00:00:00 | 0 |
30-
| Susi | 1970-01-01 00:00:00 | 0 |
31-
| Urs | 1995-01-01 00:00:00 | 0 |
32-
*/
33-
```
19+
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
if true it does a locale sensitive sorting.
1+
if true, use locale sensitive sorting.

docs/03.reference/05.objects/array/sort/_arguments/sorttype_or_closure.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ a string must be one of the following values:
88

99
if you define a closure/function, the closure/function must accept 2 parameters of any type and return:
1010

11-
-1, if first parameter is "smaller" than second parameter
12-
0, if first parameter is equal to second parameter
13-
1, first parameter is "bigger" than second parameter
11+
- -1, if first parameter is "smaller" than second parameter
12+
- 0, if first parameter is equal to second parameter
13+
- 1, first parameter is "bigger" than second parameter
14+
15+
`function (any e1, any e2) { return -1/0/1;});`
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
a list of names, a single column name or a function used as comparator.
1+
- a list of names
2+
- a single column name
3+
- or a function used as comparator with the following constructor
4+
5+
`function(struct row1, struct row2){ return true / false;}`.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
a list of directions or a single direction definition (asc,desc), the list must have the same length as the columnName list. ONly used when the second argument defines a list of column names.
1+
a list of directions or a single direction definition (asc, desc),
2+
3+
the list must have the same length as the columnName list.
4+
5+
Only used when the second argument defines a list of column names.
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
21
```luceescript+trycf
3-
news = queryNew("id,items",
4-
"integer,varchar",
5-
[ {"id":1,"items":"chocolate"}, {"id":2,"items":"juice"}, {"id":3,"items":"vegitable"} ]);
6-
//sort it
7-
news.sort("items","desc");
8-
//dump it
9-
writeDump(news);
10-
news.sort("items","asc");
11-
writeDump(news);
2+
people = QueryNew( "name,dob,age", "varchar,date,int", [
3+
[ "Susi", CreateDate( 1970, 1, 1 ), 70 ],
4+
[ "Urs" , CreateDate( 1995, 1, 1 ), 40 ],
5+
[ "Fred", CreateDate( 1960, 1, 1 ), 50 ],
6+
[ "Jim" , CreateDate( 1988, 1, 1 ), 30 ]
7+
]);
8+
9+
Dump( var=people, label="people - original query" );
10+
11+
people.sort('name', 'asc');
12+
dump(var=people, label='people - sorted by name');
13+
14+
people2 = people.sort(function(row1, row2){
15+
return compare(arguments.row1.age, arguments.row2.age);
16+
});
17+
dump(var=people2, label='people - sorted by age');
18+
1219
```

0 commit comments

Comments
 (0)