Sort-by, min-by, max-by in Nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import strutils, algorithm, future

var names = "Brian Chris Pamela Michael Tammy Michelle Roy Timothy".split()

# Find shortest and longest name
block:
  proc `<`(a, b: string): bool =
    a.len < b.len

  echo min(names), " and ", max(names)
  #< Roy and Michelle

# Sort names by length
names.sort((a, b) => cmp(a.len, b.len))
echo names
#< @[Roy, Brian, Chris, Tammy, Pamela, Michael, Timothy, Michelle]

Created (last updated )
Comments powered by Disqus