- 2 Minutes to read
- Print
- DarkLight
- PDF
Concepts: Filtering & Sorting Collections
- 2 Minutes to read
- Print
- DarkLight
- PDF
Introduction
In this article, we dive into the more intermediate concepts of filtering and sorting through a collection. This page aims to provide a variety of examples on how filters and sorting can be used instead of outlining any specific step-by-step guides. We encourage you to try out some of the examples with whatever collection commands you would like using the following templates as a guide:
For filtering, provide your filters as a string for the `Filter` input parameter
PowerShell
# Template:
# {your_collection_command} -Filter "{your_filter_string}"
# Examples:
$activeClientsCollection = Get-Clients -Filter "Active eq true"
$mattersIn2023Collection = Get-matters -Filter "year(CreatedOn) eq 2023"
$rushProjectsCollection = Get-Projects -Filter "Priority eq 'Rush'"
For sorting, provide your sorts as a string for the `OrderBy` input parameter
# Template:
# {your_collection_command} -OrderBy "{your_sort_string}"
# Examples:
$recentFirstClientsCollection = Get-Clients -OrderBy "CreatedOn desc"
$mattersSortedByClientsCollection = Get-matters -OrderBy "ClientName, Name"
$projectsSortedByStatusCollection = Get-Projects -OrderBy "Status"
You may combine filtering and sorting for the same collection command
$activeMattersWithRecentFirstCollection = Get-Matters -Filter "Active eq true" -OrderBy "CreatedOn desc"
Check out the commands and reference section of the documentation to help locate other collection commands. Remember, all collection commands start with the `Get` verb and will be plural.
Filters Reference
Below are tables that represents the filtering expressions and how to apply them to a collection command.
The following table shows comparison operators:
Operation | Operator | Examples |
---|---|---|
Equals | eq |
|
Not equals | ne |
|
Greater than | gt |
|
Greater than or equal to | ge |
|
Less than | lt |
|
Less than or equal to | le |
|
The following table shows functions that may be applied in a filter expression
Operation | Function | Examples |
---|---|---|
Contains | contains(field, value) |
|
Not contains | indexof(field, value) eq -1 |
|
Starts with | startswith(field, value) |
|
Ends with | endswith(field, value) |
|
Length | length(field) expr value |
|
Year | year(field) expr value |
|
Month | month(field) expr value |
|
Day | day(field) expr value |
|
Hour | hour(field) expr value |
|
Minute | minute(field) expr value |
|
Second | second(field) expr value |
|
All of the above operations may be grouped using parenthesis and logical `and` or `or` concatenation. Examples:
Logical group operator | Examples |
---|---|
and |
|
or |
|
Sorting (Order By) Reference
Sorting follows the following format:
field (desc)[, ...field (desc)]
For example, to sort a field named "Name" in ascending order, you would simply use:
"Name"
To sort by the same field, but in descending order:
"Name desc"
You can sort by multiple fields by separating the expressions with a comma. For example:
"Reference, ClientReference"
Reverse sorting can be applied individually on any field:
"Reference desc, ClientReference desc"
Sorting examples on a collection command
Example: Sort by the reference field on the clients collection command:
Get-Clients -OrderBy "Reference"
Example: Sort matter names but keep clients together:
Get-Matters -OrderBy "ClientName, Name"
Example: Sort billing entries by highest total price while keeping matters together:
Get-BillingEntries -OrderBy "MatterName, TotalPrice desc"