======sort====== Sort an [[PTPScript:Types:Array|Array]]. \\ **Syntax** * ''[[PTPScript:Types:Array|Array]] sort( [[PTPScript:Types:Array|Array]] subject [, [[PTPScript:Types:Boolean|Boolean]] reverse [, [[PTPScript:Types:Boolean|Boolean]] preserve [, [[PTPScript:Types:Boolean|Boolean]] byKey [, [[PTPScript:Types:Boolean|Boolean]] safe [, [[PTPScript:Types:Number|Number]] method ] ] ] ] ] )'' \\ **Parameters** * [[PTPScript:Types:Array|Array]] ''subject'' - The Array to sort. * [[PTPScript:Types:Boolean|Boolean]] ''reverse'' - Whether to reverse the sort. * **''false''** (default) - ''subject'' will be sorted from lowest to highest. * **''true''** - ''subject'' will be sorted from highest to lowest. * [[PTPScript:Types:Boolean|Boolean]] ''preserve'' - Whether to preserve item keys. * **''false''** (default) - Keys will be re-assigned. * **''true''** - Keys will be preserved. * [[PTPScript:Types:Boolean|Boolean]] ''byKey'' - Whether to sort by item key. * **''false''** (default) - ''subject'' will be sorted by item value. * **''true''** - ''subject'' will be sorted by item key. * [[PTPScript:Types:Boolean|Boolean]] ''safe'' - Whether to use "safe" sorting. * **''false''** (default) - ''subject'' will be sorted using quicksort, which is not "safe". * **''true''** - ''subject'' will be sorted using a merge sort, which is "safe". * [[PTPScript:Types:Number|Number]] ''method'' - Sort method options. * ''0'' (default) - Compare items using standard comparison. * ''1'' - Compare items numerically. * ''2'' - Compare items as case-sensitive strings. * ''3'' - Compare items as case-insensitive strings. * ''4'' - Compare items as case-sensitive strings, using [[http://sourcefrog.net/projects/natsort/|natural ordering]]. * ''5'' - Compare items as case-insensitive strings, using [[http://sourcefrog.net/projects/natsort/|natural ordering]]. * ''6'' - Compare items as case-sensitive strings, according to locale. * ''7'' - Compare items as case-insensitive strings, according to locale. \\ **Result** * [[PTPScript:Types:Array|Array]] - The sorted Array. Returns ''subject'' sorted in order according to various options. Key value, type, and order do not matter - the Array does not have to be numerically indexed. "Safe" sorting is where two identical items will retain their original order. Sometimes this is not important, and so the quickest sorting method can be used (which here is an enhanced version of quicksort). At other times, it is important for identical keys to retain their relative order, in which case the slightly slower (but safe) merge sort will be used. For more information about comparison methods, see the ''[[PTPScript:Functions:compare|compare()]]'' function.