Abstract Aim To analyse the structure of pteridophyte assemblages, based on phylogenetic relatedness and trait properties, along an elevational gradient. Ecological theory predicts that co-occurring species may be: randomly selected from a regional pool; ecologically sorted so that they are functionally different hence resulting in reduced competition (overdispersion); or functionally similar as an adaptation to specific ecological conditions (clustering). Location Braulio Carrillo National Park and Cerro de la Muerte, Costa Rica, Central America. Methods We used an empirical dataset of the quantitative pattern of species occurrences and individual numbers of ferns within 156 plots along a tropical elevational gradient to test whether directed ecological sorting might cause deviations in patterns of trait and phylogenetic diversity. Mean pairwise distances of species based on phylogenetic and trait properties were compared with two different sets of null assemblages, one maintaining species frequency distributions (constrained) and one not (unconstrained). Results Applying different null models resulted in varying degrees of overdispersion and clustering, but overall patterns of deviation from random expectations remained the same. Contrary to theoretical predictions, phylogenetic and trait diversity were relatively independent from one another. Phylogenetic diversity showed no patterns along the elevational gradient, whereas trait diversity showed significant trends for epiphytes. Main conclusions Under stressful environmental conditions (drought at low elevations and frost at high elevations), epiphytic fern assemblages tended to be clustered with respect to trait characteristics, which suggests environmental filtering. Conversely, under less extreme environmental conditions (middle of the transect), the sorting was biased towards high differentiation (overdispersion), presumably because of interspecific competition and trait shifts among closely related species (character displacement).