Solution for sorting stack