diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | array.c | 18 | ||||
-rw-r--r-- | doc/gawk.1 | 22 |
3 files changed, 41 insertions, 6 deletions
@@ -1,3 +1,10 @@ +Sun Apr 24 12:43:49 2011 John Haque <j.eh@mchsi.com> + + * array.c (sort_user_func): Fix return value to match the + documentaion. + (sort_selection): Make user-specified comparison function with + the same name override default "unsorted" specification. + Fri Apr 22 16:05:27 2011 John Haque <j.eh@mchsi.com> * array.c (sort_user_func): New routine to handle user-defined @@ -1371,8 +1371,8 @@ static int sort_user_func(const void *p1, const void *p2) { const NODE *t1, *t2; - NODE *idx1, *idx2, *val1, *val2, *r; - int ret; + NODE *idx1, *idx2, *val1, *val2; + AWKNUM ret; INSTRUCTION *code; extern int exiting; @@ -1403,11 +1403,9 @@ sort_user_func(const void *p1, const void *p2) gawk_exit(exit_val); /* return value of the comparison function */ - r = POP_SCALAR(); - ret = (int) force_number(r); - DEREF(r); + POP_NUMBER(ret); - return ret; + return (ret < 0.0) ? -1 : (ret > 0.0); } @@ -1522,6 +1520,14 @@ sort_selection(NODE *sort_str, SORT_CTXT sort_ctxt) allparts |= bval; } + if (allparts == Unsorted) { + NODE *f; + /* user-defined function overrides default */ + + if ((f = lookup(sort_str->stptr)) != NULL && f->type == Node_func) + return -1; + } + /* num_words <= 3 */ return (allparts & INDEX_MASK); @@ -1098,6 +1098,25 @@ Supported values are \fB"unsorted"\fR. The order specification words can be truncated, or omitted (provided that at least one is present), or given in any order. +The value can also be the name of any comparison function defined +as follows: +.PP +.RS +\fBfunction cmp_func(i1, v1, i2, v2)\fR +.RE +.PP +where +.I i1 +and +.I i2 +are the indices, and +.I v1 +and +.I v2 +are the +corresponding values of the two elements being compared. +It should return a number less than, equal to, or greater than 0, +depending on how the elements of the array are to be ordered. .TP \fBPROCINFO["version"]\fP the version of @@ -2475,6 +2494,9 @@ Valid values for .I how are "ascending string", "ascending number", "descending string" and "descending number". +It can also be the name of a user-defined +comparison function as described in +\fBPROCINFO["sorted_in"]\fR. .TP "\w'\fBsprintf(\^\fIfmt\fB\^, \fIexpr-list\^\fB)\fR'u+1n" \fBasorti(\fIs \fR[\fB, \fId\fR [\fB, \fIhow\fR] ]\fB)\fR Return the number of elements in the source |