1. 7.6.2.3 sort_user
- Posted by useless May 28, 2009
- 826 views
- Last edited May 29, 2009
Is it true, this function sorts all the fields in one direction? Have you looked at sorttok() and sortntok() in strtok v2.2? Either allows you to sort each field in any direction:
string = {"Red,Skelton,45,London,England", "Lucy,Ball,56,Barre,North Dakota", "Fred,Astaire,45,Alberta,Canada", "Pierre,du Pont,56,Paris,France"} string = sorttok(string,{3,-2},',') string = {"Red,Skelton,45,London,England", "Fred,Astaire,45,Alberta,Canada", "Pierre,du Pont,56,Paris,France", "Lucy,Ball,56,Barre,North Dakota"}
You see, it did a ascending sort on (fictitious) age field (field3, positive), then sorted the last name field (field2, negative) in decending order.
sortntok() is the same, but returns the sorted line numbers of string, not the lines themselves.
useless
2. Re: 7.6.2.3 sort_user
- Posted by DerekParnell (admin) May 29, 2009
- 819 views
Is it true, this function sorts all the fields in one direction?
The documentation is a bit misleading. The custom_sort orders the result determined by the value returned by the user-defined comparision routine. The order argument can be used to reverse the sense of the user defined routine's value.
In other words, when the user defined routine returns a negative value, the order=DESCENDING parameter causes the sort to behave as if the routine had returned a postive value.
I've updated the documentation to make this more clearer.
Have you looked at sorttok() and sortntok() in strtok v2.2? Either allows you to sort each field in any direction:
string = {"Red,Skelton,45,London,England", "Lucy,Ball,56,Barre,North Dakota", "Fred,Astaire,45,Alberta,Canada", "Pierre,du Pont,56,Paris,France"} string = sorttok(string,{3,-2},',') string = {"Red,Skelton,45,London,England", "Fred,Astaire,45,Alberta,Canada", "Pierre,du Pont,56,Paris,France", "Lucy,Ball,56,Barre,North Dakota"}
You see, it did a ascending sort on (fictitious) age field (field3, positive), then sorted the last name field (field2, negative) in decending order.
sortntok() is the same, but returns the sorted line numbers of string, not the lines themselves.
These are nice routines. We already have a sort_columns() routine but it works on sequences that represent records - each element is a field. In your case, the sequences are text with comma-delimited fields.
I'll have a go at adding your ideas into the library ... maybe this weekend.