sortmulti

Sort an Array according to columns.


Syntax


Parameters

  • Array subject - The Array to sort. This Array should contain sub-Arrays as rows, each of which should contain the columns named for sorting.
  • Array options - The sort options. This parameter should consist of an Array of options, where each option is itself an Array containing the option details. The order of the options dictates the sort order.
    • Array option - Each sort option should be an Array containing numerically-indexed option details.
      • 0: Mixed column - The name of the column to sort by, where the name is a valid Array key (i.e. a String or an Integer).
      • 1: 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.
      • 2: 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.
  • Boolean preserve - Whether to preserve item keys.
    • false (default) - Keys will be re-assigned.
    • true - Keys will be preserved.


Result

  • Array - The sorted Array.

Returns subject sorted in order, by column, according to various options.

Key value, type, and order do not matter - the Array does not have to be numerically indexed.

The sortmulti() function uses "safe" sorting, that is, two identical items will retain their original order.

For more information about comparison methods, see the compare() function.

Examples

To demonstrate how to use the sortmulti() function, let's first set up an Array with some example data:

{ Users = [
    ["Name": "Joe",  "Admin": false],
    ["Name": "Bill", "Admin": true],
    ["Name": "Jim",  "Admin": false],
    ["Name": "Adam", "Admin": false],
    ["Name": "Zak",  "Admin": false],
    ["Name": "Greg", "Admin": true]
  ]
}

Now let's create some code to display the contents of the Array:

{ macro Display(Users) }{ loop Users as User }{ User.Name }
{ endloop }{ endmacro }

Now we can display the users by running the macro:

{ Display(Users) }

  Joe
  Bill
  Jim
  Adam
  Zak
  Greg

Let's sort the users by name first:

{ SortedUsers = sortmulti(Users, [["Name"]]) }{ Display(SortedUsers) }

  Adam
  Bill
  Greg
  Jim
  Joe
  Zak

And now let's sort them so that the admin users are listed first:

{ SortedUsers = sortmulti(Users, [["Admin", true]]) }{ Display(SortedUsers) }

  Greg
  Bill
  Zak
  Adam
  Jim
  Joe

Notice that because true is greater than false, we had to sort in reverse.

Now let's combine both sorts by sorting by Admin and then by Name:

{ SortedUsers = sortmulti(Users, [["Admin", true], ["Name"]]) }{ Display(SortedUsers) }

  Bill
  Greg
  Adam
  Jim
  Joe
  Zak

Hopefully that shows how simple, yet powerful, the sortmulti() function is.

ptpscript/functions/sortmulti.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