Fix bug, add else part, $max_int, and change sort() to ksort() -refs BT#8185