move sorting logic from controller into model