moved filtering before sorting