.NET Forum / Languages / C# / March 2008
Sorting a list by DateTime stamp -- please help...
|
|
Thread rating:  |
almurph@altavista.com - 19 Mar 2008 12:22 GMT Hi,
I have a list object called "myList" with the first item a string and the second a DateTime. Something like this:
"some text 1", 19/03/2008 12:56:36 "some text 2", 19/03/2008 10:56:36 "some text 3", 19/03/2008 11:56:36 "some text 4", 19/03/2008 13:56:36
I would like to sort "myList" (into a new list) by timestamp such that the earliest time stamp is a tthe bottom. In this case it would be:
"some text 2", 19/03/2008 10:56:36
Problem is though I don't know how to do this exactly! Would appreciate any comments/suggestions/code-samples that you would liek to share.
Thanking you, Al.
Jon Skeet [C# MVP] - 19 Mar 2008 12:39 GMT > I have a list object called "myList" with the first item a string and > the second a DateTime. Something like this: [quoted text clipped - 3 lines] > "some text 3", 19/03/2008 11:56:36 > "some text 4", 19/03/2008 13:56:36 Do you mean you've got a list of lists, or a list of some type which encapsulates a string and a DateTime?
> I would like to sort "myList" (into a new list) by timestamp such > that the earliest time stamp is a tthe bottom. In this case it would [quoted text clipped - 5 lines] > appreciate any comments/suggestions/code-samples that you would liek > to share. What version of .NET are you using? This is a lot easier in .NET 3.5...
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
almurph@altavista.com - 19 Mar 2008 12:48 GMT > almu...@altavista.com <almu...@altavista.com> wrote: > > I have a list object called "myList" with the first item a string and [quoted text clipped - 23 lines] > Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet > World class .NET training in the UK:http://iterativetraining.co.uk Hi John,
Version 2.0
Jon Skeet [C# MVP] - 19 Mar 2008 13:22 GMT <snip>
> Version 2.0 And the answer to the data type question?
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
almurph@altavista.com - 19 Mar 2008 13:36 GMT > almu...@altavista.com <almu...@altavista.com> wrote: > [quoted text clipped - 7 lines] > Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet > World class .NET training in the UK:http://iterativetraining.co.uk Sorry John - didn't see the question - yes I have a class which encapulated the data in a wrapper
public class aList { public string myString; public DateTime updated;
public CacheList( string myString, DateTime updated) { this.myString = myString; this.updated = updated; }
}
Any suggestions?
Jon Skeet [C# MVP] - 19 Mar 2008 13:54 GMT > Sorry John - didn't see the question - yes I have a class which > encapulated the data in a wrapper [quoted text clipped - 13 lines] > > Any suggestions? Sure. (For a start, I'd avoid public fields, but that's a different matter.)
To sort a list in place, use:
list.Sort (delegate (CacheList first, CacheList second) // Note ordering - this is to get earliest at bottom { return second.updated.CompareTo(first.updated); } );
If you want to sort it as a *new* list, copy the list first:
List<CachedList> sorted = new List<CachedList>(list); sorted.Sort (same code as above)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
almurph@altavista.com - 19 Mar 2008 14:05 GMT > almu...@altavista.com <almu...@altavista.com> wrote: > > Sorry John - didn't see the question - yes I have a class which [quoted text clipped - 35 lines] > > - Show quoted text - Thanks for that John - your a star. Concerning using public field - can you explain? Any recommendations?
Interested, Al.
Jon Skeet [C# MVP] - 19 Mar 2008 14:26 GMT <snip>
> Thanks for that John - your a star. No problem. Are you reasonably happy with the anonymous method syntax?
(Just for reference, using C# 3 and .NET 3.5 it really is trivial:
List<CachedList> sorted = list.OrderByDescending(item => item.updated) .ToList();
:)
> Concerning using public field - > can you explain? Any recommendations? I'd suggest using properties to expose the data for "getters" - and quite possibly make the type immutable at the same time. That has various benefits, not least when it comes to multi-threading.
At some point I need to write up an article about why properties are much better than public fields, and another about the benefits of immutability (although the latter is documented in various places perfectly well).
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
almurph@altavista.com - 19 Mar 2008 16:47 GMT > almu...@altavista.com <almu...@altavista.com> wrote: > [quoted text clipped - 3 lines] > > No problem. Are you reasonably happy with the anonymous method syntax? Wish I knew what that meant!
> (Just for reference, using C# 3 and .NET 3.5 it really is trivial: > [quoted text clipped - 9 lines] > quite possibly make the type immutable at the same time. That has > various benefits, not least when it comes to multi-threading. You mean something like this:
public class aList { private string myString; public MyString { get { return myString; } }
private DateTime updated; public Updated { get { return updated; } }
public CacheList( string myString, DateTime updated) { this.myString = myString; this.updated = updated; }
}
Sorry - don't knwo how to make the type immutable! Help!
> At some point I need to write up an article about why properties are > much better than public fields, and another about the benefits of [quoted text clipped - 4 lines] > Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet > World class .NET training in the UK:http://iterativetraining.co.uk Jon Skeet [C# MVP] - 19 Mar 2008 16:56 GMT > > No problem. Are you reasonably happy with the anonymous method syntax? > > Wish I knew what that meant! The delegate (...) { ... } stuff.
See http://pobox.com/~skeet/csharp/csharp2/delegates.html
> > I'd suggest using properties to expose the data for "getters" - and > > quite possibly make the type immutable at the same time. That has > > various benefits, not least when it comes to multi-threading. > > You mean something like this: Yup, although with a name which matches the constructor :)
> public class aList > { [quoted text clipped - 18 lines] > > Sorry - don't knwo how to make the type immutable! Help! You've already done it :)
You could make it "even more immutable" (which isn't quite as daft as it sounds) by making the fields readonly - generally a good idea where possible.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
almurph@altavista.com - 19 Mar 2008 17:26 GMT > almu...@altavista.com <almu...@altavista.com> wrote: > > > No problem. Are you reasonably happy with the anonymous method syntax? > > > Wish I knew what that meant! > > The delegate (...) { ... } stuff. Sure. Had to change is slightly to make it work for your suggestions but seems okay. Why is there a better way? Something I'm missing? Some better way? Suggestions/comments appreciated Jon.
> Seehttp://pobox.com/~skeet/csharp/csharp2/delegates.html > [quoted text clipped - 5 lines] > > Yup, although with a name which matches the constructor :) Done. thanks.
> > public class aList > > { [quoted text clipped - 24 lines] > it sounds) by making the fields readonly - generally a good idea where > possible. Done. Thanks Jon
> -- > Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet > World class .NET training in the UK:http://iterativetraining.co.uk- Hide quoted text - > > - Show quoted text - Jon Skeet [C# MVP] - 19 Mar 2008 18:15 GMT > > almu...@altavista.com <almu...@altavista.com> wrote: > > > > No problem. Are you reasonably happy with the anonymous method syntax? [quoted text clipped - 6 lines] > but seems okay. Why is there a better way? Something I'm missing? Some > better way? Suggestions/comments appreciated Jon. If you're using C# 2, this is probably about as good as it gets IMO, unless you use a custom comparer which applies a simpler predicate to each element each time... that involves more work, but would make it simpler to implement the next time you had to do something similar.
Unfortunately I don't have the time right now to explain myself more - if no-one else has done it later, I'll do it myself :)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
sloan - 19 Mar 2008 18:40 GMT // If you're using C# 2, this is probably about as good as it gets IMO, unless you use a custom comparer which applies a simpler predicate to each element each time... that involves more work, but would make it simpler to implement the next time you had to do something similar. //
(To the OP) Follow the below link...read a little bit, and it'll show the Predicate and simple comparer.
You would be very well served to read: http://ludwig-stuyck.spaces.live.com/Blog/cns!E36D9BA98FC913B3!398.entry
the article on Generics.
It is in english.
..
Find the Sort section.
almurph@altavista.com <almurph@altavista.com> wrote:
> On Mar 19, 3:56 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote: > > almu...@altavista.com <almu...@altavista.com> wrote: [quoted text clipped - 8 lines] > but seems okay. Why is there a better way? Something I'm missing? Some > better way? Suggestions/comments appreciated Jon. If you're using C# 2, this is probably about as good as it gets IMO, unless you use a custom comparer which applies a simpler predicate to each element each time... that involves more work, but would make it simpler to implement the next time you had to do something similar.
Unfortunately I don't have the time right now to explain myself more - if no-one else has done it later, I'll do it myself :)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Jon Skeet [C# MVP] - 19 Mar 2008 19:01 GMT > // > If you're using C# 2, this is probably about as good as it gets IMO, [quoted text clipped - 6 lines] > Follow the below link...read a little bit, and it'll show the Predicate and > simple comparer. I followed the link but didn't find quite what I was expecting. I also used the wrong word when I said predicate - I meant projection.
Basically, it's feasible to write an IComparer which you'd create instances of like this:
IComparer<Person> comp = ProjectionComparer.Create(Person p => p.Name);
Or something like that...
I'll see if I've got time to implement it later.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Jon Skeet [C# MVP] - 19 Mar 2008 21:27 GMT > > Sure. Had to change is slightly to make it work for your suggestions > > but seems okay. Why is there a better way? Something I'm missing? Some [quoted text clipped - 7 lines] > Unfortunately I don't have the time right now to explain myself more - > if no-one else has done it later, I'll do it myself :) Okay, this is the kind of thing I meant. I've trimmed some of the XML comments just because they ended up wrapping. The non-generic classes are used to help with type inference. The only use for MiscUtil.Extensions is for projection.ThrowIfNull - you can convert that into if (projection==null) { throw new ArgumentNullException("projection"); }
if you want.
using System; using System.Collections.Generic; using System.Linq; using System.Text;
using MiscUtil.Extensions; using System.Collections;
namespace MiscUtil.Collections { /// <summary> /// Non-generic class to produce instances of the generic class, /// optionally using type inference. /// </summary> public static class ProjectionComparer { public static ProjectionComparer<TSource, TKey> Create<TSource,TKey>(Func<TSource, TKey> projection) { return new ProjectionComparer<TSource, TKey>(projection); }
public static ProjectionComparer<TSource, TKey> Create<TSource,TKey> (TSource ignored, Func<TSource, TKey> projection) { return new ProjectionComparer<TSource, TKey>(projection); }
}
/// <summary> /// Class generic in the source only to produce instances of the /// doubly generic class, optionally using type inference. /// </summary> public static class ProjectionComparer<TSource> { public static ProjectionComparer<TSource, TKey> Create<TKey>(Func<TSource, TKey> projection) { return new ProjectionComparer<TSource, TKey>(projection); } }
public class ProjectionComparer<TSource,TKey> : IComparer<TSource> { readonly Func<TSource, TKey> projection;
public ProjectionComparer(Func<TSource, TKey> projection) { projection.ThrowIfNull("projection"); this.projection = projection; }
public int Compare(TSource x, TSource y) { // Don't want to project from nullity if (x==null && y==null) { return 0; } if (x==null) { return -1; } if (y==null) { return 1; } return Comparer<TKey>.Default.Compare(projection(x), projection(y)); } } }
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
sloan - 19 Mar 2008 15:13 GMT You would be very well served to read: http://ludwig-stuyck.spaces.live.com/Blog/cns!E36D9BA98FC913B3!398.entry
the article on Generics.
It is in english.
..
Find the Sort section.
..
You can write the comparer "inline" (previous post on this thread) ... or you can actually code up the the Comparer.
..
> Hi, > [quoted text clipped - 18 lines] > Thanking you, > Al.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|