templates: Abstract sort logic into separate class