The understanding of when and how ecological and evolutionary processes structure communities is a central goal in ecology. Both evolutionary changes within community members and shifts in species composition via species sorting can contribute to the dynamics of community traits and composition over time. However, we currently lack an understanding of when and how species sorting and evolution contribute to community dynamics. Here, we estimated contributions of species sorting and evolution over time (60 days) in bacterial communities consisting of 24 species under selection by a protozoan predator Tetrahymena thermophila. We found that species sorting contributed to the communities' carrying capacity, while evolution contributed to its anti-predator defences, and that relative roles of both processes changed over time. In contrast to previous observations from single bacterial populations, bacterial defences declined rapidly. We also found that manipulating the evolutionary history of predator and prey (as a phenotypic match-mismatch combination) affected species sorting and evolution. Community composition, population densities and genomic evolution differed contingent on predator and prey history. Overall, our analysis indicates that the relative roles of ecology and evolution depended on the initial trait variation present in the community. If variation in the direction of selection was present species sorting prevailed, otherwise evolution drove phenotypic change.