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

时间:2011-03-17 00:48:28

标签: c# linq sorting lambda

如何通过Person中选择的字典值对其进行排序?

示例:“人员”的订单列表由“cars”值降序。

这可能与其中一个奇特的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" } }
    }
};

编辑/示例结果:

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

人 - &gt;汽车 - &gt; {“volvo”,“34023200”}
人 - &gt;汽车 - &gt; {“volvo”,“3554000”}
人 - &gt;汽车 - &gt; {“volvo”,“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" } }
    }
};

2 个答案:

答案 0 :(得分:4)

这是一个漂亮的技巧;基本上,字典可以被认为是KeyValuePair的列表&lt;&gt;对象。

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();

当然,更大的问题是为什么你需要对Dictionary进行排序和排序 - 这并不是一个真正与字典概念组合在一起的操作(它更适合通过指定键快速访问特定元素) 。你写的问题描述似乎很奇怪;你可以为特定的人订购汽车,但是你是否试图找到按价值订购的每个人的每辆车?

这也很容易做到:

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();

答案 1 :(得分:0)

如果您的意思是按照该人拥有的汽车数量排序

var result = Person.OrderByDescending(x => x.cars.Count);

如果您的意思是按照该人拥有的第一个(按名称排序)汽车名称排序

var result = Person.OrderByDescending(x => x.cars.Keys.OrderBy(y => y.Key).FirstOrDefault());

如果您的意思是按人员拥有的第一个(按号码排序)车号排序

var result = Person.OrderByDescending(x => x.cars.Keys.OrderBy(y => y.value).FirstOrDefault());

一个示例输出可以很好地阐明您实际想要排序的内容,因为汽车是一个列表,而不仅仅是一个元素。


根据您提供的示例,这可能只是为您完成工作:

var result = Person.OrderByDescending(x => x.cars.Values.FirstOrDefault());
相关问题