sort

Sort an Array.


Syntax


Parameters

  • Array subject - The Array to sort.
  • 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.
  • Boolean preserve - Whether to preserve item keys.
    • false (default) - Keys will be re-assigned.
    • true - Keys will be preserved.
  • 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.
  • 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".
  • 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 natural ordering.
    • 5 - Compare items as case-insensitive strings, using natural ordering.
    • 6 - Compare items as case-sensitive strings, according to locale.
    • 7 - Compare items as case-insensitive strings, according to locale.


Result

  • 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 compare() function.

ptpscript/functions/sort.txt · Last modified: 2007/01/24 10:15
 
 
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki