简体   繁体   English

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

[英]How to Sort a List of Objects by dictionary value?

How would I sort this by a chosen dictionary value in Person? 如何通过Person中选择的字典值对其进行排序?

Example: Order List of "Person" by "cars" value descending. 示例:“人员”的订单列表由“汽车”值降序。

Is that possible with one of those fancy lambda/linq equations? 这可能与那些奇特的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" } }
    }
};

EDIT / EXAMPLE RESULT: 编辑/示例结果:

The result would be a new or reordered list based on a value contained in the dictionary of each Person 结果将是基于每个Person的字典中包含的值的新列表或重新排序列表

Person -> cars -> { "volvo", "34023200" } 人 - >汽车 - > {“volvo”,“34023200”}
Person -> cars -> { "volvo", "3554000" } 人 - >汽车 - > {“volvo”,“3554000”}
Person -> cars -> { "volvo", "340050" } 人 - >汽车 - > {“volvo”,“340050”}

The new or reordered List would look exactly like this: 新的或重新排序的列表看起来完全像这样:

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" } }
    }
};

Here's a nifty trick; 这是一个漂亮的技巧; basically, a Dictionary can be thought of as a List of KeyValuePair<> objects. 基本上,Dictionary可以被认为是KeyValuePair <>对象的List。

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

Now that it is in a list, it's easy to sort via LINQ: 现在它在列表中,很容易通过LINQ排序:

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

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

Of course, the bigger problem is why you would need to sort and order a Dictionary - that's not exactly an operation that is really grouped with the concept of a dictionary (it's more for quick access of specific elements via a specified key). 当然,更大的问题是为什么你需要对Dictionary进行排序和排序 - 这并不是一个真正与字典概念组合在一起的操作(它更适合通过指定键快速访问特定元素)。 The problem description you write about seems odd; 你写的问题描述似乎很奇怪; you can order the cars for a specific person, but are you trying to find each car from each person ordered by value? 您可以为特定的人订购汽车,但是您是否想要按价值订购的每个人找到每辆车?

That's easy to do as well: 这也很容易做到:

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

If you mean sort by the number of cars owned by the person 如果你的意思是按照这个人拥有的汽车数量排序

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

If you mean sort by the first (sorted by name) car name owned by the person 如果您的意思是按人员拥有的第一个(按名称排序)汽车名称排序

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

If you mean sort by the first (sorted by number) car number owned by the person 如果您的意思是按人员拥有的第一个(按号码排序)车号排序

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

An example output would be good to clarify what actually you want to sort by, since cars is a list, rather than just an element. 一个示例输出可以很好地阐明您实际想要排序的内容,因为汽车是一个列表,而不仅仅是一个元素。


Based on the examples you provided this might just do the job for you: 根据您提供的示例,这可能只是为您完成工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM