Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
Sort iterable objects
The order by
clause in the query expression can be used to sort the elements in a collection. Ordering works consistently with the <
, <=
, >
, >=
operators. Some comparisons involving ()
and float NaN
are considered unordered. Therefore, if these unordered types are encountered in the query, they will be returned as the last elements of the ordered collection.
The syntax to write an order by
clause is order by expression orderDirection
. The order direction can be ascending
or descending
.
import ballerina/io;
type Employee record {
string firstName;
string lastName;
decimal salary;
};
public function main() {
Employee[] employees = [
{firstName: "Jones", lastName: "Welsh", salary: 1000.00},
{firstName: "Anne", lastName: "Frank", salary: 5000.00},
{firstName: "Rocky", lastName: "Irving", salary: 6000.00},
{firstName: "Anne", lastName: "Perera", salary: 3000.00},
{firstName: "Jermaine", lastName: "Perera", salary: 4000.00},
{firstName: "Rocky", lastName: "Puckett", salary: 6000.00},
{firstName: "Jermaine", lastName: "Kent", salary: 4000.00}
];
Employee[] sorted = from var e in employees
// The `order by` clause sorts the output items based on the given
// `order-key` and the `order-direction`.
// The `order-key` must be an ordered type.
// The `order-direction` is `ascending` if not specified explicitly.
order by e.firstName ascending, e.lastName descending
select e;
foreach Employee e in sorted {
io:println(e.firstName, " ", e.lastName);
}
}
$ bal run sort_iterable_objects.balAnne PereraAnne FrankJermaine PereraJermaine KentJones WelshRocky PuckettRocky Irving
Related links
PreviousQuery expressions
NextLet clause