Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

advanced sorting

(1) Example #1
words = ["ccc", "aaa", "d", "bb"]

print(sorted(words))  # ['aaa', 'bb', 'ccc', 'd']

# now, sort by length:
# --------------------

print(sorted(words, key=len))
# ['d', 'bb', 'ccc', 'aaa']

def myFunc(s):
    return len(s)

print(sorted(words, key=myFunc))
# ['d', 'bb', 'ccc', 'aaa']
import std/algorithm  # sorted(seq), reversed(seq)
import std/sugar      # =>


let words = @["ccc", "aaa", "d", "bb"]

echo sorted(words)  # @["aaa", "bb", "ccc", "d"]

# now, sort by length:
# --------------------

echo sorted(words, (a, b) => a.len - b.len)
# @["d", "bb", "ccc", "aaa"]

func myCmp(s, t: string): int =
  s.len - t.len

echo sorted(words, myCmp)
# @["d", "bb", "ccc", "aaa"]

Nim uses a comparator, i.e. it compares two elements. The return value of the comparator is similar to the strcmp() function in C. If you compare x and y, then:

  • if x < y, returns a negative value (< 0)
  • if x > y, returns a positive value (> 0)
  • if x == y, returns 0

There's also a generic compare proc called cmp:

proc cmp[T](x, y: T): int

Returns:

  • a value less than zero, if x < y
  • a value greater than zero, if x > y
  • zero, if x == y
(2) Example #2
words = ["Cc", "BB", "aa", "zz"]

print(sorted(words))  # ['BB', 'Cc', 'aa', 'zz']

# now, sort in ignore-case fashion:
# ---------------------------------

print(sorted(words, key=str.lower))
# ['aa', 'BB', 'Cc', 'zz']
import std/algorithm  # sorted(seq), reversed(seq)
import std/strutils   # toLowerAscii


let words = @["Cc", "BB", "aa", "zz"]

echo sorted(words)  # @["BB", "Cc", "aa", "zz"]

# now, sort in ignore-case fashion:
# ---------------------------------

func myCmp(s, t: string): int =
  let a = s.toLowerAscii
  let b = t.toLowerAscii
  return cmp(a, b)

echo sorted(words, myCmp)
# @["aa", "BB", "Cc", "zz"]

𝥶Here, calling cmp() with two strings works like calling strcmp() in C.

(3) Example #3
words = ["xc", "zb", "yd", "wa"]

print(sorted(words))  # ['wa', 'xc', 'yd', 'zb']

# now, sort by the last character:
# --------------------------------

print(sorted(words, key=lambda s: s[-1]))
# ['wa', 'zb', 'xc', 'yd']
import std/algorithm  # sorted(seq), reversed(seq)
import std/sugar      # =>


let words = @["xc", "zb", "yd", "wa"]

echo sorted(words)  # @["wa", "xc", "yd", "zb"]

echo sorted(words, (a, b) => ord(a[^1]) - ord(b[^1]))
# @["wa", "zb", "xc", "yd"]

Anonymous functions

An anonymous function is a function that has no name. In Python it's called a lambda function.

Let's see how to write one in Nim:

import std/algorithm  # sorted(seq), reversed(seq)
import std/sugar      # =>


let numbers = @[8, 5, 1, 3]

echo sorted(numbers)    # @[1, 3, 5, 8]

# using a comparator with the `=>` operator
echo sorted(numbers, (a, b) => a - b)    # @[1, 3, 5, 8]

# using a comparator with an anonymous function
echo sorted(numbers, func(a, b: int): int = a - b)  # @[1, 3, 5, 8]
  • As you can see, an anonymous function looks like a normal function, simply its name is missing.
  • The anonymous function is the long form. You can make it shorter with the `=>` operator, but it's just a syntactic sugar for constructing an anonymous function.
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 May 07, 09:09