Skip to main content
sort(list) returns a new list with all elements sorted in ascending lexicographic order.

Signature

sort(list list(string)) list(string)

Example

Sort a list of service names alphabetically before printing a deployment summary, so the output is consistent across runs regardless of declaration order:
variable "services" {
  description = "Services to include in the release"
  type        = list(string)
  default     = ["worker", "api", "scheduler", "gateway"]
}

computed "sorted_services" {
  description = "Services in alphabetical order"
  expression  = sort(var.services)
}

task "release-summary" {
  description = "Print the list of services included in this release"
  commands = [
    "echo 'Services in this release: ${join(", ", computed.sorted_services)}'",
  ]
}
Output: Services in this release: api, gateway, scheduler, worker.

Notes

  • sort only works on list(string). For numbers, convert to strings first or sort within shell commands.
  • The sort is lexicographic, not numeric. "10" sorts before "9" when treated as strings.
  • sort returns a new list and does not modify the original.