listSort: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Applied Template:MacroFunction)
m (Fix formatting on return value in example.)
 
(11 intermediate revisions by 6 users not shown)
Line 2: Line 2:
{{MacroFunction
{{MacroFunction
|name=listSort
|name=listSort
|description
|description=
Returns a sorted list.
Returns a sorted list.


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[listSort(list, sortType)]
[listSort(list, sortType)]
</source>
[listSort(list, sortType, delim)]
</syntaxhighlight>
 
'''Parameters'''
{{param|list|The list that is sorted.}}
{{param|sortType|The direction that this list is sorted. "A+" - alpha ascending, "A-" - alpha descending, "N-" - padded ending numbers descending alpha}}
{{param|delim|The delimiter used in the separate the list, defaults to {{code|","}}.}}
 
The sortType determines the type of sort to use.
The sortType determines the type of sort to use.
If sortType is "A", normal alphabetic sorting is used, and "Monster11" comes before "Monster3". (Default behavior)
If sortType is "A", normal alphabetic sorting is used, and "Monster11" comes before "Monster3". (Default behavior)
If sortType is "N", the first number in each entry is effectively padded to 4 digits, so that "Monster3" comes before "Monster11".
If sortType is "N", the first number in each entry is effectively padded to 4 digits, so that "Monster3" comes before "Monster11".
The sortType can have a second character of "+" or "-" to specify an ascending or descending sort.
The sortType can have a second character of "+" or "-" to specify an ascending or descending sort.
<strong>Note about numeric lists</strong>: String list values are strings, not numbers. This function can sort lists of non-negative numbers correctly with or without prefixes such as "Monster1", "Monster2", etc. Negative numbers cannot be ordered in reverse order since the hyphen is viewed as a string prefix, not as a negation symbol. In other words, the string list "-5, 1, -3, 0" will be sorted to "-3, -5, 0, 1".
If you want to order a list of numbers, use [[json.sort]] on a JSON Array. For more information about this limitation, check [https://github.com/RPTools/maptool/issues/3878 the related issue on GitHub].
|example=
<syntaxhighlight lang="mtmacro" line>
[h: UnsortedList = "Monster11,Monster3,Monster12,Monster66,Monster87,Monster71"]
[h: SortedList = listSort(UnsortedList,'N')]
Unsorted list: [r: UnsortedList]<br>
Sorted list: [r: SortedList]
</syntaxhighlight>
Returns:
<syntaxhighlight lang="mtmacro" line>
  Unsorted list: Monster11,Monster3,Monster12,Monster66,Monster87,Monster71
  Sorted list: Monster3, Monster11, Monster12, Monster66, Monster71, Monster87
</syntaxhighlight>
}}
}}
[[Category:String List Function]]
[[Category:String List Function]]

Latest revision as of 23:59, 21 March 2023

 This article is a stub, you can help the RPTools Wiki project by contributing content to expand this article.

listSort() Function

Returns a sorted list.

Usage

[listSort(list, sortType)]
[listSort(list, sortType, delim)]

Parameters

  • list - The list that is sorted.
  • sortType - The direction that this list is sorted. "A+" - alpha ascending, "A-" - alpha descending, "N-" - padded ending numbers descending alpha
  • delim - The delimiter used in the separate the list, defaults to ",".

The sortType determines the type of sort to use. If sortType is "A", normal alphabetic sorting is used, and "Monster11" comes before "Monster3". (Default behavior) If sortType is "N", the first number in each entry is effectively padded to 4 digits, so that "Monster3" comes before "Monster11". The sortType can have a second character of "+" or "-" to specify an ascending or descending sort.

Note about numeric lists: String list values are strings, not numbers. This function can sort lists of non-negative numbers correctly with or without prefixes such as "Monster1", "Monster2", etc. Negative numbers cannot be ordered in reverse order since the hyphen is viewed as a string prefix, not as a negation symbol. In other words, the string list "-5, 1, -3, 0" will be sorted to "-3, -5, 0, 1".

If you want to order a list of numbers, use json.sort on a JSON Array. For more information about this limitation, check the related issue on GitHub.

Example

[h: UnsortedList = "Monster11,Monster3,Monster12,Monster66,Monster87,Monster71"]
[h: SortedList = listSort(UnsortedList,'N')]

Unsorted list: [r: UnsortedList]<br>
Sorted list: [r: SortedList]

Returns:

  Unsorted list: Monster11,Monster3,Monster12,Monster66,Monster87,Monster71
  Sorted list: Monster3, Monster11, Monster12, Monster66, Monster71, Monster87