Skip to main content

並べ替え(sort).比較関数を指定

Python

py
sorted(items, mycmp) # -1, 0, 1 を返す関数を自作
sorted(items, lambda x, y: cmp(x.lower(), y.lower())) # cmp を返す

比較関数を指定する場合、

条件戻り値
x < y-1
x == y0
x > y1

それは return cmp(a, b) することで可能

Swift

swift
items.sorted(by: Closure)
swift
items.sorted(by: { $0.lowercased() < $1.lowercased() })

Ruby

rb
arr1.sort {|a, b| a.downcase <=> b.downcase } # <=> を返す