Local binary pattern (LBP) is sensitive to inverse grayscale changes. To overcome this problem, several methods map each LBP code and its complement to the minimum one. However, without distinguishing LBP codes and their complements, these methods show limited description ability. In this paper, we introduce a generic histogram sorting method which exploits pattern transition rules to preserve the distribution information of LBP codes and their complements. Based on this method, we develop a series of sorted LBP (SLBP) descriptors, including pairwise sorted ones and fully sorted ones, which are all invariant to grayscale inversion and image rotation. Since SLBP focuses on encoding difference-sign information, it is further generalized to embed difference-magnitude LBP features to obtain complementary representations. We also propose an invariant pyramid pooling strategy to aggregate SLBP features into a pyramid image representation. Experiments on several benchmark texture databases and one newly collected image database (grayscale-inversion images, GII) demonstrate the effectiveness of our descriptors for image classification under (linear or nonlinear) grayscale-inversion and rotation changes. The source code will be available at https://github.com/stc-cqupt/slbp.