c# - 如何按字典值对对象列表进行排序?

标签 c# linq sorting lambda

我如何根据 Person 中选定的字典值对其进行排序?

示例:按“cars”值降序排列“Person”列表。

使用那些奇特的 lambda/linq 方程之一可能吗?

public class People 
{
    List<Person> Persons { get; set; }
}

public class Person
{
    public Dictionary<string, string> cars { get; set; } 
    public Dictionary<string, string> houses { get; set; } 
    public Dictionary<string, string> banks { get; set; }
}

................

People people = new People();

people.Persons = new List<Person> 
{
    new Person 
    {
        cars = new Dictionary<string, string> { { "volvo", "340050" }, { "bmw", "50545000" } }, 
        houses = new Dictionary<string, string> { { "mansion", "100040000" },{ "cardboard box", "112" } },
        banks = new Dictionary<string, string> { { "swiss", "12500330000" }, { "camen", "12000000" } }
    }, 
    new Person 
    {
        cars = new Dictionary<string, string> { { "volvo", "34023200" }, { "bmw", "5003300" } }, 
        houses = new Dictionary<string, string> { { "mansion", "1000330000" },{ "cardboard box", "277" } },
        banks = new Dictionary<string, string> { { "swiss", "12500044000" }, { "camen", "12000000" } }
    }, 
    new Person 
    {
        cars = new Dictionary<string, string> { { "volvo", "3554000" }, { "bmw", "50023200" } }, 
        houses = new Dictionary<string, string> { { "mansion", "1006600000" },{ "cardboard box", "244" } },
        banks = new Dictionary<string, string> { { "swiss", "125000544000" }, { "camen", "12000777000" } }
    }
};

编辑/示例结果:

结果将是一个新的或重新排序的列表,该列表基于每个人的字典中包含的值

人物 -> 汽车 -> { "volvo", "34023200"
人 -> 汽车 -> { “沃尔沃”,“3554000”
人 -> 汽车 -> {“沃尔沃”,“340050”}

新的或重新排序的列表看起来完全像这样:

people.Persons = new List<Person> 
{
    new Person 
    {
        cars = new Dictionary<string, string> { { "volvo", "34023200" }, { "bmw", "5003300" } }, 
        houses = new Dictionary<string, string> { { "mansion", "1000330000" },{ "cardboard box", "277" } },
        banks = new Dictionary<string, string> { { "swiss", "12500044000" }, { "camen", "12000000" } }
    },
    new Person 
    {
        cars = new Dictionary<string, string> { { "volvo", "3554000" }, { "bmw", "50023200" } }, 
        houses = new Dictionary<string, string> { { "mansion", "1006600000" },{ "cardboard box", "244" } },
        banks = new Dictionary<string, string> { { "swiss", "125000544000" }, { "camen", "12000777000" } }
    },
    new Person 
    {
        cars = new Dictionary<string, string> { { "volvo", "340050" }, { "bmw", "50545000" } }, 
        houses = new Dictionary<string, string> { { "mansion", "100040000" },{ "cardboard box", "112" } },
        banks = new Dictionary<string, string> { { "swiss", "12500330000" }, { "camen", "12000000" } }
    }
};

最佳答案

这是一个绝妙的技巧;基本上,字典可以被认为是 KeyValuePair<> 对象的列表。

Dictionary<int, string> myDictionary = new Dictionary<int, string>();
foreach(var item in myDictionary)
// item is a KeyValuePair<int, string>, enumerated in order of their insertion into the dictionary

现在它在列表中,很容易通过 LINQ 进行排序:

Dictionary<int, string> sample = new Dictionary<int, string>
{
   { 1, "Sample" },
   { 2, "Another Sample" }
};

var orderedList = sample.OrderBy(x => x.Value).ToList();

当然,更大的问题是为什么你需要对字典进行排序和排序——这并不是真正与字典概念分组的操作(它更多的是通过指定的键快速访问特定元素) .您写的问题描述似乎很奇怪;您可以为特定的人订购汽车,但您是否要尝试按值(value)从每个订购的人那里找到每辆汽车?

这也很容易做到:

List<PersonCarDetails> allStuff = new List<PersonCarDetails>();

foreach(var person in persons)
   allStuff.AddRange(person.Cars.Select(x => new PersonCarDetails { PersonId = person.Id, CarName = x.Key, CarId = x.Value }).ToList());

 // allStuff now contains a list of PersonCarDetails, and then you can order that:

 var orderedList = allStuff.OrderBy(x => x.CarId).ToList();

关于c# - 如何按字典值对对象列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5333571/

相关文章:

arrays - 有序数组中重复的数字

c# - 如何将 DataTable 转换为 List<KeyValuePair<string, int>>

c# - "x => { throw .. }"的 Lambda 推断匹配重载方法中的 Func<T,Task>?

c# - 将常见的 Razor 助手移动到另一个文件

c# - 即使一组为空,获取笛卡尔积的 LINQ 查询是什么?

java - QuickSort 在线程 "main"java.lang.StackOverflowError 中给出异常

c# - 模拟 java 的 Card Layout Manager 的 Tables 选项卡控件

c# - 正确处理 lambda 表达式中可能出现的 System.NullReferenceException

c# - 通过将 Customer.UserName 与登录用户匹配来查找 CustomerID

java - 选择排序从两端开始增长有序范围