Puppet Function: sort

Defined in:
vendor_modules/stdlib/lib/puppet/parser/functions/sort.rb
Function type:
Ruby 3.x API

Summary

Sorts strings and arrays lexically.

Overview

sort()Any

Note that from Puppet 6.0.0 the same function in Puppet will be used instead of this.

Returns:

  • (Any)

    sorted string or array



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'vendor_modules/stdlib/lib/puppet/parser/functions/sort.rb', line 8

newfunction(:sort, type: :rvalue, doc: <<-DOC
  @summary
    Sorts strings and arrays lexically.

  @return
    sorted string or array

  Note that from Puppet 6.0.0 the same function in Puppet will be used instead of this.
DOC
) do |arguments|
  if arguments.size != 1
    raise(Puppet::ParseError, "sort(): Wrong number of arguments given #{arguments.size} for 1")
  end

  value = arguments[0]

  if value.is_a?(Array)
    value.sort
  elsif value.is_a?(String)
    value.split('').sort.join('')
  end
end