Sorting of terms and contributions