簡體   English   中英

如何按字典值對對象列表進行排序?

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

如何通過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" } }
    }
};

編輯/示例結果:

結果將是基於每個Person的字典中包含的值的新列表或重新排序列表

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

這是一個漂亮的技巧; 基本上,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

現在它在列表中,很容易通過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();

如果你的意思是按照這個人擁有的汽車數量排序

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM