1

I couldn't express better what I am asking in the title.

This is what I'm looking.

I have a disordered List of an Specific Object I have a DateTime and String Property.

The String Property Has values Like this ones (note that it is an string, not a number, it always has the K letter, I should be ordering with just the numbers):

K07000564, 
K07070000
K07069914
K07026318
K07019189

What I want is to order the List By Date... but when ordering if the String value is present in the collection with other Date I want to order them just after this one (By Date also in that miniGroup of IdFinders)... and then keep ordering...

Something Like this:

Edit

I edited the example to clarify that ordering by IdFinder will not work... I need to order By Date.. if when ordering by Date the IdFinder is present more than once in the collection should show them just after this last one, and then keep ordering the rest of them and so on by each idfinder

ID         Date
**K07000564**   Today
K07000562   Yesterday
K07000563   The Day Before Yesterday
**K07000564** The day before the day before yesterday

Should be

K07000564 Today
K07000564 The day before the day before yesterday
K07000562 Yesterday 
K07000563  The Day Before Yesterday 

I achieved this in SQL Server 2008 in a project before with something like this:

WITH B
AS
(
    SELECT 
        ID, 
        MAX(DATE_COLUMN) DATE_COLUMN, 
        ROW_NUMBER() OVER (ORDER BY MAX(DATE_COLUMN) DESC) RN
    FROM MYTABLE
    GROUP BY ID

)

SELECT *
FROM MYTABLE c
, B
WHERE ID= b.ID
ORDER BY b.rn, c.DATE_COLUMN desc;

But I'm not good with Linq and I have no idea of how doing this in Linq.

Maybe an Important Note I'm in .NEt 2.0, so no LINQ available but I'm using Linqbridge to use Linq.

I tried this, but as you will notice, this will not work

oList.OrderBy(i => i.IdFinder).ThenByDescending(i => i.OperationDate);

I hope to have explained this clearly

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
Hector Sanchez
  • 2,297
  • 4
  • 26
  • 39

3 Answers3

5
var result = oList.OrderByDescending(x => x.OperationDate)
                  .GroupBy(x => x.IdFinder)
                  .SelectMany(x => x);
I4V
  • 34,891
  • 6
  • 67
  • 79
2

I think this should do the trick:

var sortedList = oList
    .GroupBy(x => x.IdFinder)
    .Select(g =>
        new
            {
                MaxOpDate = g.Max(x => x.OperationDate),
                Items = g
            })
    .OrderByDescending(g => g.MaxOpDate)
    .SelectMany(g => g.Items.OrderByDescending(x => x.OperationDate));

However, I haven't tested it with Linqbridge.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
  • I think you also need to order the items in each group. – svick Dec 28 '12 at 19:13
  • it works w0lf it takes a while let me try @I4V answer, + 1, thanks for your help – Hector Sanchez Dec 28 '12 at 21:40
  • @Mr. yes, I hope I4V's solution works because it should be faster, as it only sorts once. – Cristian Lupascu Dec 28 '12 at 21:44
  • I think they bot woth work, but they produce different result, maybe because the dates are just dates, without time, and LInq Order them different in both querys,anyway both of them are correct but I4V is faster as you said. Thank you anyway for your help – Hector Sanchez Dec 28 '12 at 21:56
0

Try this

oList.OrderBy(i => int.parse(i.IdFinder.Substring(1,i.IdFinder.Length-1)).ThenByDescending(i => i.OperationDate);

First extract the numeric value out of IdFinder, the orderby this value.

Note -> It is assumed that i.IdFinder is always a valid "K######" where # is number less than Int32.MaxValue.

Tilak
  • 30,108
  • 19
  • 83
  • 131