使用 Linq 对 Person 对象列表进行排序
我需要根据对象属性的类别对对象列表进行查询。我需要这些组的顺序不同于我在许多其他示例中看到的通常的字母顺序。我正在使用从其他地方获取的示例。如何根据 HomeProvince 但按照以下顺序生成 Person 对象列表:
安大略省、魁北克省、艾伯塔省、马尼托巴省、不列颠哥伦比亚省。每个组内的顺序并不重要。
Person[] people = new Person[]
{
new Person() { FirstName = "Tony", LastName = "Montana", Age = 39, HomeProvince = "Ontario" },
new Person() { FirstName = "Bill", LastName = "Smith", Age = 23, HomeProvince = "Ontario" },
new Person() { FirstName = "Jane", LastName = "Doe", Age = 23, HomeProvince = "Alberta" },
new Person() { FirstName = "John", LastName = "Doe", Age = 23, HomeProvince = "Alberta" },
new Person() { FirstName = "Alex", LastName = "DeLarge", Age = 19, HomeProvince = "British Columbia" },
new Person() { FirstName = "Travis", LastName = "Bickle", Age = 42, HomeProvince = "Quebec" },
new Person() { FirstName = "Ferris", LastName = "Beuller", Age = 17, HomeProvince = "Manitoba" },
new Person() { FirstName = "Maggie", LastName = "May", Age = 23, HomeProvince = "Ontario" },
new Person() { FirstName = "Mickey", LastName = "Mouse", Age = 93, HomeProvince = "Alberta" },
new Person() { FirstName = "Frank", LastName = "Darabont", Age = 49, HomeProvince = "Ontario" }
};
I need a query on sorting list of objects based on the category of property of the objects. I need the groups to be in an order other than the usual alphabetical order which I have seen in many other samples. I am using an example I took from elsewhere. How can I generate a list of Person objects based on HomeProvince but in terms of this ordering:
Ontario, Quebec, Alberta, Manitoba, British Columbia. The ordering within each group does not matter.
Person[] people = new Person[]
{
new Person() { FirstName = "Tony", LastName = "Montana", Age = 39, HomeProvince = "Ontario" },
new Person() { FirstName = "Bill", LastName = "Smith", Age = 23, HomeProvince = "Ontario" },
new Person() { FirstName = "Jane", LastName = "Doe", Age = 23, HomeProvince = "Alberta" },
new Person() { FirstName = "John", LastName = "Doe", Age = 23, HomeProvince = "Alberta" },
new Person() { FirstName = "Alex", LastName = "DeLarge", Age = 19, HomeProvince = "British Columbia" },
new Person() { FirstName = "Travis", LastName = "Bickle", Age = 42, HomeProvince = "Quebec" },
new Person() { FirstName = "Ferris", LastName = "Beuller", Age = 17, HomeProvince = "Manitoba" },
new Person() { FirstName = "Maggie", LastName = "May", Age = 23, HomeProvince = "Ontario" },
new Person() { FirstName = "Mickey", LastName = "Mouse", Age = 93, HomeProvince = "Alberta" },
new Person() { FirstName = "Frank", LastName = "Darabont", Age = 49, HomeProvince = "Ontario" }
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
这基本上会:
如果您需要按省份分组的人员,那也很简单:
另一种选择是创建从省份到“优先级”的映射并简单地按该顺序排序。这实际上取决于您需要什么作为输出。
You could do this:
That will basically:
If you need the people grouped by province, that's easy too:
Another option would be to create a mapping from province to "priority" and simply order by that. It really depends on exactly what you need as output.
我不确定这是否是最好的方法,但我可能会让每个以安大略省为家乡的人并将他们添加到数组中。然后重复魁北克等......我不知道任何其他方式,因为你需要的是“随机”。
我说数组是因为这就是您在代码示例中使用的数组。显然,只要您不在放入物品时或放入之后订购它们,那么使用哪种容器并不重要。
I'm not sure if this is the best way but I would probably just get every person with Ontario as HomeProvince and add them to the array. Then repeat for Quebec and so on... I don't know any other way because what you need is "random".
I say array because that's what you used in your code sample. Obviously it doesn't matter what kind of container you use as long as you don't order them as you put them in or after they are in.