Sort output by criteria