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

发布于 2024-10-24 06:42:29 字数 2950 浏览 2 评论 0原文

我如何根据 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 ->汽车 -> {“沃尔沃”,“34023200”}
人->汽车 -> {“沃尔沃”,“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" } }
    }
};

How would I sort this by a chosen dictionary value in Person?

Example: Order List of "Person" by "cars" value descending.

Is that possible with one of those fancy lambda/linq equations?

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 -> cars -> { "volvo", "34023200" }
Person -> cars -> { "volvo", "3554000" }
Person -> cars -> { "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" } }
    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-10-31 06:42:29

这是一个巧妙的技巧;基本上,字典可以被认为是 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();

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

这也很容易做到:

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

Here's a nifty trick; basically, a Dictionary can be thought of as a List of KeyValuePair<> objects.

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:

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). 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();
老旧海报 2024-10-31 06:42:29

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

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

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


根据您提供的示例,这可能适合您:

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

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文