Swan Lake Beta2 is here.
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}
    ];

    Employee[] sorted = from var e in employees
                        // The `order by` clause sorts the output items based on the
                        // given `order-key` and `order-direction`. The `order-key`
                        // must be an `ordered` type. The `order-direction` is `ascending`
                        // if not specified explicitly.
                        order by e.lastName ascending, e.firstName ascending


                        select e;
    io:println(sorted);
}

Ordering

Ordering works consistently with <, <=, >, >= operators. Some comparisons involving () and float NaN are considered unordered. order by clause allows expressions not just field access. A library module can enable Unicode-aware sorting by providing a unicode:sortKey(str, locale) function.

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}
    ];
    Employee[] sorted = from var e in employees
                        order by e.lastName ascending, e.firstName ascending

The order by clause sorts the output items based on the given order-key and order-direction. The order-key must be an ordered type. The order-direction is ascending if not specified explicitly.

                        select e;
    io:println(sorted);
}
bal run ordering.bal
[{"firstName":"Anne","lastName":"Frank","salary":5000.00},{"firstName":"Jones","lastName":"Welsh","salary":1000.00}]
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}
    ];

    Employee[] sorted = from var e in employees
                        // The `order by` clause sorts the output items based on the
                        // given `order-key` and `order-direction`. The `order-key`
                        // must be an `ordered` type. The `order-direction` is `ascending`
                        // if not specified explicitly.
                        order by e.lastName ascending, e.firstName ascending


                        select e;
    io:println(sorted);
}

Ordering

Ordering works consistently with <, <=, >, >= operators. Some comparisons involving () and float NaN are considered unordered. order by clause allows expressions not just field access. A library module can enable Unicode-aware sorting by providing a unicode:sortKey(str, locale) function.

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}
    ];
    Employee[] sorted = from var e in employees
                        order by e.lastName ascending, e.firstName ascending

The order by clause sorts the output items based on the given order-key and order-direction. The order-key must be an ordered type. The order-direction is ascending if not specified explicitly.

                        select e;
    io:println(sorted);
}
bal run ordering.bal
[{"firstName":"Anne","lastName":"Frank","salary":5000.00},{"firstName":"Jones","lastName":"Welsh","salary":1000.00}]

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand