智能排序列表c#
本文关键字:列表 排序 智能 | 更新日期: 2023-09-27 18:05:16
my code:
IEnumerable<Blob> bb = from element in bloblist
let locationx = element.Rectangle.X
let locationy = element.Rectangle.Y
orderby locationy ascending ,locationx descending
select element;
foreach (var b0 in bb)
{
listBox1.Items.Add(b0.Rectangle.X + " " + b0.Rectangle.Y);
}
它的工作正常,但我的目的:
1。顺序位置升序,locationx降序
2。如果location [i+1]-location [i]<10
3。然后:order locationx降序,location升序
4.继续的例子:
Sorted(x,y) Desired
--------- ---------
x y x y
30 0 35 7
35 7 30 0
15 20 30 27
25 25 25 25
30 27 15 20
有人能帮我吗?
很难知道你在做什么,但你可能想尝试将你的问题简化成不同排序的离散步骤,并将它们连接在一起:
IEnumerable<Blob> bb =
from element in bloblist
where condition
orderby whatever
select element;
IEnumerable<Blob> bb2 =
from element in bb
where condition2
orderby whatever2
select element;
甚至:
IEnumerable<Blob> bb3 =
from element in bb.Concat(bb2)
where condition2
orderby whatever2
select element;
我怀疑将复杂的排序标准简化为内置IEnumerable方法的组合总是可能的。
很难确切地说出您要的是什么,但看起来应该可以得到您想要的值:
bloblist.Select(item => new { X = item.Rectangle.X, Y = item.Rectangle.Y })
.OrderByDescending(item => item.X)
.ToList()
.ForEach(item =>
{
listBox1.Items.Add(item.X + " " + item.Y);
});
这应该会给你一组(X,Y)对,按X值降序排序。