From WikiChip
$sorttok identifier - mIRC
< mirc‎ | identifiers
Revision as of 02:26, 30 March 2020 by Maroonbells (talk | contribs) (addl examples)

$sorttok sorts the list of tokens. $sorttokcs is the case sensitive version

Synopsis

$sorttok(<list>,C[,nrca])
$sorttokcs(<list>,C[,nrca])

Parameters

  • <list> - The list of tokens (can be empty which causes output to also be $null)
  • <C> - The ascii number (or code point) of the character separating the tokens
  • ncra - default is alphabetical sort (ASCII order) if 3rd parameter not used, n = numeric sort; c = channel prefix sort; r = reverse; a = ASCII sort

Note: 'a' sort is case-insensitive. For case-insensitive use $sorttokcs Note: 'n' sort makes no attempt to sort non-numerics, so using the numeric sort in $sorttokcs has no effect.

Properties

None

Examples

//echo -a $sorttok(C a c b A B,32,a)

Result is "a A b B C c" because the a-vs-A and c-vs-C letters are output in the same sequence in which they appear in the input.

As shown by the following, the 'a' switch is the default, and Alpha sort order is in ASCII order except for handling A-Z and a-z as equivalents, which makes the Alpha sort order for $sorttokcs be ASCII order. The only difference between $sorttok and $sorttokcs is whether codepoint ranges for a-z and A-Z are equivalent to each other:

//var -s %a $regsubex(foo,$str(x,1223),/x/g,$chr($calc(0+ \n)) $chr(32)) , %b $sorttok(%a,32) | echo -a $strip(%b) | echo 4 -a  %b ==> $chr(15) $chr(22) $regsubex(foo,%b,/([^ ])/gu, $asc(\t) )
alias NumericMax { return $gettok($sorttok($1- , 32 , rn),1,32) }
alias NumericMin { return $gettok($sorttok($1- , 32 ,  n),1,32) }

Note: the above NumericMax assumes the input contains ONLY numbers, because in a numeric sort, non-numeric is treated as if zero, and is sorted between the positives vs the negative numbers. It appears that $sorttok's numeric sort evaluates each token individually the same was as evaluated by $int().

//echo -a min: $NumericMin(1 2 11) max: $NumericMax(1 2 11)

This means non-numerics are chopped from the end of the token if the token begins with a number, though it treats 'e' or 'E' as potentially being part of a number in scientific notation. Tokens 2+2 or +2*2 or 2-2 are treated the same as '2' because that's the number prior to encountering a non-numeric. All tokens which evaluate to the same number appear to always be kept in the order they appear in the input, so $sorttok(64 64.0,32,n) and $sorttok(64.0 64,32,n) both return an unchanged string.

Leading zero and non-numerics don't affect the numeric sort order:
//echo -a $sorttok( 1:black 02:blue 14:grey 3:green 13:magenta 5:maroon 6:purple 04:red 7:tan 8:yellow 0:white ,32,n)
result:
0:white 1:black 02:blue 3:green 04:red 5:maroon 6:purple 7:tan 8:yellow 13:magenta 14:grey
 
//var %a $regsubex($str(x,94),/x/g,$chr($calc(32+ \n)) $chr(32)) | echo -a $sorttokcs(11 -11 -1 1 +1 -2 2 $true $false $true -x1xa 0 $false +2*2 0 0.0 %a 5-1 5-11 5-2 2+2 =4 101 99 1E2xx $false 0.1 1d2,32,n)
 
Returns: -11 -2 -1 $true $false $true -x1xa 0 $false 0 0.0 ! " # $ % & ' ( ) * + , - . / 0 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ =4 $false 0.1 1 +1 1 2 +2*2 2 2+2 3 4 5 5-1 5-11 5-2 6 7 8 9 11 99 1E2xx 1d2 101

Note: The first $true through the last $false appear in the output as the same order as in the input because they all evaluate as zero. The same happens to the several tokens which all evaluate as 2 or 5. 1E2xx sorts between 99 and 101 because it evaluates 1E2 as scientific notation for 100.

Note: The 'r' switch is a modifier which reverses the direction of the sort for the a/n/c sort orders. For numeric sort, it does not always put the tokens into inverse order. It only changes the direction of the sort after each token has been evaluated to its numeric equivalent. Using 'r' by itself is the same as 'ar' because 'a' is the default sort order.

Note: Using the 'c' switch instead of the default 'a' changing the sort order to a modified ASCII order matching the sort order of the nicklist for that network. Tokens beginning with any of the channel prefix symbols are sorted to the front. These prefixes are shown by the $prefix identifier, so the above display will be different in a blank status window created with "/server -n" that it would be at a network which recognizes ~ and & as nick prefixes.

For example, the display of the following string depends on the value of $prefix in the window where that command executes:

//echo -a prefix: $prefix network: $network sort: $sorttok( ~owner &protectedop @op % $+ halfop +voice [nick] \nick\ ^nick^ anick znick |nick|,32,c)
Possible Results:
prefix: prefix: @+ network: freenode sort: @op +voice %halfop &protectedop [nick] \nick\ ^nick^ anick znick |nick| ~owner
prefix: ~&@%+ network: Rizon sort: ~owner &protectedop @op %halfop +voice [nick] \nick\ ^nick^ anick znick |nick|

Compatibility

Added: mIRC v4.7
Added on: 09 Dec 1996
Note: Unless otherwise stated, this was the date of original functionality.
Further enhancements may have been made in later versions.

See also