gpt4 book ai didi

c# - 如何先按一个属性排序,然后再按另一个属性排序,而不考虑第一个属性的关系?

转载 作者:行者123 更新时间:2023-11-30 16:01:45 24 4
gpt4 key购买 nike

我想要实现的是首先按最高 AmountOrderSummary 进行排序,然后显示集合中的所有其他 Order无论 Amount 一个接一个地为给定的帐户。预期结果在代码片段中..

 public class OrderSummary
{
public string FirstName { get; set; }

public decimal Amount { get; set; }
}

public class Worker
{
public List<OrderSummary> Orders { get; set; }

public Worker()
{
Orders = new List<OrderSummary>()
{
new OrderSummary() {FirstName = "James", Amount = 10.00m},
new OrderSummary() {FirstName = "Thomas", Amount = 11.00m},
new OrderSummary() {FirstName = "Leon", Amount = 13.00m},
new OrderSummary() {FirstName = "Lori", Amount = 14.00m},
new OrderSummary() {FirstName = "Thomas", Amount = 16.00m},
new OrderSummary() {FirstName = "Thomas", Amount = 6.00m},
new OrderSummary() {FirstName = "James", Amount = 19.00m}
};
}

//sorted by highest amount first
//then place firstname together regardless of the amount

//Expected Outcome
/*
James 19 -- highest amount followed by all other orders for James regardless of the amount.
* james 10
* thomas 16
* thomas 11
* thomas 6
* lori 14
* leon 13
*/
}

我的方法是获取所有出现不止一次的元素,然后定位除第一个元素外的元素在哪个索引处,将其从索引中移除,并将其添加到第一个元素的索引 + 1.. 是否有更好的方法来完成这个?

最佳答案

GroupBy 按照它们在源集合中出现的顺序创建组,因此您可以先按数量排序,然后按名称分组,然后“展平”组:

var results = Orders.OrderByDescending(o => o.Amount)
.GroupBy(o => o.FistName) // the groupings will be in order of the largest amount
.SelectMany(g => g); // flatten the groups

关于c# - 如何先按一个属性排序,然后再按另一个属性排序,而不考虑第一个属性的关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com