Algosim documentation: SortBy

SortBy

Sorts a container by a particular property of the elements.

Syntax

Description

If X is a container and f a function on X, then SortBy(X, f) returns X sorted using the default comparer applied to the images of the elements of X under f.

In other words,

SortBy(X, f) = CustomSort(X, CompareValue(f(left), f(right)))

and

SortBy(X, identity) = sort(X).

Examples

Alice ≔ ExampleData("Alice in Wonderland");

WordWrap(
  join(
    last(
      SortBy(
        unique(
          filter(
            words(Alice),
            s ↦ ∀(s, (c ↦ ChrIsLetter(c) ∧ ChrIsLowerCase(c)))
          )
        ),
        length
      ),
      20
    )
  )
)
occasionally, difficulties, straightened, explanations, thoughtfully,
thunderstorm, neighbouring, consultation, nevertheless, hippopotamus,
straightening, uncomfortable, circumstances, extraordinary, inquisitively,
uncomfortably, conversations, contemptuously, disappointment, affectionately
person ≔ (fn, ln, role) ↦ struct("FirstName": fn, "LastName": ln, "role": role);
L ≔ '(person("Albus", "Dumbledore", "headmaster"), person("Minerva", "McGonagall", "teacher"), person("Severus", "Snape", "teacher"), person("Pomona", "Sprout", "teacher"), person("Filius", "Flitwick", "teacher"), person("Horace", "Slughorn", "teacher"), person("Harry", "Potter", "student"), person("Ronald", "Weasley", "student"), person("Hermione", "Granger", "student"), person("Remus", "Lupin", "teacher"), person("Rubeus", "Hagrid", "teacher"), person("Luna", "Lovegood", "student"))
(FirstName: Albus, LastName: Dumbledore, role: headmaster)
(FirstName: Minerva, LastName: McGonagall, role: teacher)
(FirstName: Severus, LastName: Snape, role: teacher)
(FirstName: Pomona, LastName: Sprout, role: teacher)
(FirstName: Filius, LastName: Flitwick, role: teacher)
(FirstName: Horace, LastName: Slughorn, role: teacher)
(FirstName: Harry, LastName: Potter, role: student)
(FirstName: Ronald, LastName: Weasley, role: student)
(FirstName: Hermione, LastName: Granger, role: student)
(FirstName: Remus, LastName: Lupin, role: teacher)
(FirstName: Rubeus, LastName: Hagrid, role: teacher)
(FirstName: Luna, LastName: Lovegood, role: student)
SortBy(L, member("LastName"))
(FirstName: Albus, LastName: Dumbledore, role: headmaster)
(FirstName: Filius, LastName: Flitwick, role: teacher)
(FirstName: Hermione, LastName: Granger, role: student)
(FirstName: Rubeus, LastName: Hagrid, role: teacher)
(FirstName: Luna, LastName: Lovegood, role: student)
(FirstName: Remus, LastName: Lupin, role: teacher)
(FirstName: Minerva, LastName: McGonagall, role: teacher)
(FirstName: Harry, LastName: Potter, role: student)
(FirstName: Horace, LastName: Slughorn, role: teacher)
(FirstName: Severus, LastName: Snape, role: teacher)
(FirstName: Pomona, LastName: Sprout, role: teacher)
(FirstName: Ronald, LastName: Weasley, role: student)

See also