试试这个,不需要比较器
// Setup test data
var orderList = new List<int> { 2, 1, 3 };
var stuffList = new List<Stuff> {
new Stuff { Id = 1, Name = "Apple" },
new Stuff { Id = 2, Name = "Banana" },
new Stuff { Id = 3, Name = "Test" }
};
// Do sort according to list
var result = orderList.Select(idx => stuffList.Where(s => s.Id == idx));
编辑:创建 ID 查找可能会更快:
var stuffDictionary = stuffList.ToDictionary(s => s.ID, s => s);
var result = orderList.Where(idx => stuffDictionary.ContainsKey(idx))
.Select(idx => stuffDictionary[idx]);