智能排序列表c#

时间:2011-08-09 19:53:47

标签: c# sorting

我的代码:

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.orderby locationy ascending,locationx descending

2.if locationy [i + 1] -locationy [i]&lt; 10

3.then:orderby locationx下降,locationy ascending

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

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

很难说出你想要做什么,但是你可能想尝试将问题简化为不同排序的离散步骤并将它们链接在一起:

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方法的组合。

答案 1 :(得分:0)

很难准确说出你的要求,但看起来这应该能让你达到预期的价值:

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值下降排序。

相关问题