Sort the variables and preserve selection