File rpmsort of Package rpm
#!/bin/sh
# rpmsort implemented mostly in Lua
# Copyright (c) 2020 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later
# Author: fvogt@suse.de
# Enhanced by: mwilck@suse.com
rpmsort() {
	direction=$1
	script='
function parse(ver)
	local epoch, version, release = 0, ver, 0
	_, eend, e = ver:find("^(%d+):")
	if eend then
		ver = ver:sub(eend + 1)
		version = ver
		epoch = e
	end
	_, _, v, r = ver:find("(.+)%-(.+)$")
	if v then
		version = v
		release = r
	end
	return epoch, version, release
end
function pkgvercmp(a, b)
	local ae, av, ar = parse(a)
	local be, bv, br = parse(b)
	local ecmp = rpm.vercmp(ae, be)
	if ecmp ~= 0 then return ecmp end
	local vcmp = rpm.vercmp(av, bv)
	if vcmp ~= 0 then return vcmp end
	return rpm.vercmp(ar, br)
end
vers = {}
for line in io.stdin:lines() do
	table.insert(vers, line)
end
table.sort(vers, function(a, b) return pkgvercmp(a, b) == '"$direction"' end)
print(table.concat(vers, "\n"))
'
	rpm --eval "%{lua: ${script}}"
}
usage() {
	cat >&2 <<EOF
$0 - sort input according to rpm version sorting conventions
Expects rpm package versions separated by newlines as input and outputs
them sorted according to rpm version sorting conventions, with old versions
at the top.
Options:
	-r|--reverse	sort backwards
	-h|--help	print this help
EOF
	exit 0
}
DIRECTION=-1
while [ $# -gt 0 ]; do
	case $1 in
		-r|--reverse) DIRECTION=1;;
		-h|--help) usage;;
		*)  echo "$0: invalid parameter $1 ignored" >&2;;
	esac
	shift
done
rpmsort "$DIRECTION"