开发者

intelligent sorting list c#

my code :

IEnumerable<Blob> bb = from element in bloblist
    let locationx = element.Rectangle.X
    let locationy =开发者_JAVA技巧 element.Rectangle.Y
    orderby locationy  ascending ,locationx  descending
    select element;
foreach (var b0 in bb)
{
    listBox1.Items.Add(b0.Rectangle.X + "    " + b0.Rectangle.Y);
}

it's work correctly but my purpose:

1.orderby locationy ascending ,locationx descending

2.if locationy[i+1]-locationy[i]<10

3.then : orderby locationx descending , locationy ascending

4.continue

example:

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

could anyone help me?


It's very hard to tell what you're trying to do, but you might want to try simplifying your problem into differently-sorted discrete steps and linking them together:

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;

or even:

IEnumerable<Blob> bb3 =
from element in bb.Concat(bb2)
where condition2
orderby whatever2
select element;

I suspect that it's always possible to simplify complicated sort criteria as a composition of the built-in IEnumerable methods.


It's hard to tell exactly what you are asking for, but it looks like this should get you to the desired value:

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);
        });

That should give you a set of (X,Y) pairs sorted by descending X value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜