sort array by terms