如何按字典值对对象列表进行排序?
我如何根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个巧妙的技巧;基本上,字典可以被认为是 KeyValuePair<> 的列表。对象。
既然它在列表中,就可以很容易地通过 LINQ 进行排序:
当然,更大的问题是为什么您需要对字典进行排序和排序 - 这并不完全是一个真正与字典概念分组的操作(它是更多用于通过指定键快速访问特定元素)。你写的问题描述看起来很奇怪;您可以为特定的人订购汽车,但是您是否想按价值从每个人那里找到每辆车?
这也很容易做到:
Here's a nifty trick; basically, a Dictionary can be thought of as a List of KeyValuePair<> objects.
Now that it is in a list, it's easy to sort via LINQ:
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:
如果您的意思是按此人拥有的汽车数量排序
如果您的意思是按此人拥有的第一个(按名称排序)汽车名称排序
如果您的意思是按此人拥有的第一个(按数字排序)汽车号码
排序示例输出可以很好地阐明您实际想要排序的内容,因为 cars 是一个列表,而不仅仅是一个元素。
根据您提供的示例,这可能适合您:
If you mean sort by the number of cars owned by the person
If you mean sort by the first (sorted by name) car name owned by the person
If you mean sort by the first (sorted by number) car number owned by the person
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: