我有一个列表,其中包含一些BW具有2个 属性的对象,分别为shape和quantity。
根据preShape值和preQuantity给定的值,我将使用和将每个项目排序。所述优先级为第一,则是第二优先级。我已经对和进行了排序,然后对进行了排序,并取决于和。这样的结果很好:List<BW>preShapepreQuantityshapequantityList<BW>GroupByshapequantitypreShapepreQuantity
| shape | quantity|
--------|----------
| MN22 | 20 |
| MN22 | 14 |
| MN11 | 20 |
| MN11 | 10 |
| ANT | 20 |
| ANT | 18 |
| ANT | 16 |
| ANT | 10 |
Run Code Online (Sandbox Code Playgroud)
但我想这样List<BW>:
| shape | quantity|
--------|----------
| MN22 | 20 |
| MN22 | 14 |
| MN11 | 10 |
| MN11 | 20 |
| ANT | 20 |
| ANT | 10 |
| ANT | 16 |
| ANT | 18 |
Run Code Online (Sandbox Code Playgroud)
我尝试使用for循环和swap函数进行排序,但结果与我想要的结果不同。
我想按列表排序,结果类似于第二张表。
我需要组形状,使他们在一组,我会比较形状在列表中的每个项目与预形状,如果他们匹配,我将排序列表与形预则数量将是第二个条件。但是,组A中最后一项的数量后必须跟相同数量的组B中的第一项。如果它们不匹配,则组B中第一项的数量将按ASC排序。
我在此处附加我的代码以获取更多描述 我的dottnetfiddle代码
public static void Main()
{
List<BW> lst = new List<BW>(){
new BW(){ shape = "MN11", quantity = 20},
new BW(){ shape = "MN11", quantity = 10},
new BW(){ shape = "MN22", quantity = 14},
new BW(){ shape = "MN22", quantity = 20},
new BW(){ shape = "ANT", quantity = 16},
new BW(){ shape = "ANT", quantity = 18},
new BW(){ shape = "ANT", quantity = 20},
new BW(){ shape = "ANT", quantity = 10}
};
string preShape = "MN22";
int preQuantity = 20;
var tempList = lst.GroupBy(c=>c.shape).Select(g=> new {shapeGroup = g.Key, BW = g.OrderBy(c=> c.shape.Equals(preShape)).ThenByDescending(c=>c.quantity)}).OrderByDescending(g=>g.shapeGroup.Equals(preShape));
//var tempList = lst.GroupBy(c=>c.shape).Select(g=> new {shapeGroup = g.Key, BW = g.OrderByDescending(c => c.shape.Equals(preShape))}).OrderBy(g=>g.BW.First().quantity == preQuantity);;
foreach (var shape in tempList)
{
var lastGroupBW_Item = shape.BW.LastOrDefault();
//Console.WriteLine("{0}", shape.shape);
foreach (var bw in shape.BW)
{
if(shape.BW.ToList().IndexOf(bw) > 0 )
{
shape.BW.OrderBy(c=>c.quantity == lastGroupBW_Item.quantity).ThenBy(c=>c.quantity);
}
Console.WriteLine("{0}|{1}", bw.shape,bw.quantity);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的最后一个结果就像上面描述的第二张表。提前致谢!
因此,的项目shape == preShape应位于顶部和顶部quantity == preQuantity;您可以bool为此排序(请注意false < true):
List<BW> lst = ...
var result = lst
.OrderByDescending(item => item.shape != preShape) // preShape on the top
.ThenByDescending(item => item.shape) // in case of tie by shape
.ThenByDescending(item => item.quantity != preQuantity) // preQuantity on the top
.ThenBy(item => item.quantity); // in case of tie by quantity
Run Code Online (Sandbox Code Playgroud)
演示:(我Tuple为了模拟而使用BW)
List<(string shape, int quantity)> lst = new List<(string shape, int quantity)>(){
( shape :"MN11", quantity : 20),
( shape :"MN11", quantity : 10),
( shape :"MN22", quantity : 14),
( shape :"MN22", quantity : 20),
( shape : "ANT", quantity : 16),
( shape : "ANT", quantity : 18),
( shape : "ANT", quantity : 20),
( shape : "ANT", quantity : 10),
};
string preShape = "MN22";
int preQuantity = 20;
var result = lst
.OrderByDescending(item => item.shape == preShape) // preShape on the top
.ThenByDescending(item => item.shape) // in case of tie by shape
.ThenByDescending(item => item.quantity == preQuantity) // preQuantity on the top
.ThenBy(item => item.quantity); // in case of tie by quantity
Console.Write(string.Join(Environment.NewLine, result));
Run Code Online (Sandbox Code Playgroud)
结果:
(MN22, 20) // preShape is the first to come; preQuantity is on the top within MN22 shape
(MN22, 14)
(MN11, 20) // preQuantity is on the top within the MIN11 shape
(MN11, 10)
(ANT, 20) // preQuantity is on the top within the ANT shape
(ANT, 10)
(ANT, 16)
(ANT, 18)
Run Code Online (Sandbox Code Playgroud)