Sorting in general with generic list
In one of my previous posts (caching cleanup strategy) I described a specific problem I have to solve for SharedCache, the .Net caching solution for distributed cache: http://www.sharedcache.com/ / http://www.codeplex.com/SharedCache
The following example here is just partly, the full code will be available within a few day's on CodePlex - upon my next hugh checkin .... people i'm a bit afraid to check it in sometimes it has so many new features ;-) - that i say as a person which preferres the way of: make small steps your reach your target faster!!!!
a simple sample class (a full formatted working class sample from my project): 1: public class Cleanup : IComparable<Cleanup>
2: {
3: string yourProperty = string.Empty;
4: public enum SortingOrder
5: {
6: /// <summary>
7: /// ascending sorting order
8: /// </summary>
9: Asc,
10: /// <summary>
11: /// descending sorting order
12: /// </summary>
13: Desc
14: }
15: public int CompareTo(Cleanup other)
16: {
17: return this.yourProperty.CompareTo(other.yourProperty);
18: }
19: public static Comparison<Cleanup> CacheItemPriority =
20: delegate(Cleanup cu1, Cleanup cu2)
21: {
22: if (Cleanup.Sorting == SortingOrder.Asc)
23: {
24: return cu1.yourProperty.CompareTo(cu2.yourProperty);
25: }
26: else
27: {
28: return cu2.yourProperty.CompareTo(cu1.yourProperty);
29: }
30: };
31: }
Once you have defined your class, you are able to sort it like need, with Ascending and Descending sortings
the sample you find also formatted at here: http://www.ronischuetz.com/code/cachecleanup.html
1: public class CacheCleanup
2: {
3: public CacheCleanup()
4: {
5: List<Cleanup> coll = new List<Cleanup>();
6: coll.Add(new Cleanup(IndexusMessage.CacheItemPriority.Normal, new TimeSpan(0, 1, 7), 1, new DateTime(2007, 12, 28, 14, 12, 12), 21232, 90));
7: coll.Add(new Cleanup(IndexusMessage.CacheItemPriority.AboveNormal, new TimeSpan(0, 15, 7), 2, new DateTime(2007, 12, 28, 14, 12, 14), 22232, 190));
8: coll.Add(new Cleanup(IndexusMessage.CacheItemPriority.BelowNormal, new TimeSpan(0, 18, 7), 3, new DateTime(2007, 12, 28, 14, 12, 40), 23232, 88));
9: coll.Add(new Cleanup(IndexusMessage.CacheItemPriority.High, new TimeSpan(0, 8, 7), 4, new DateTime(2007, 12, 28, 14, 12, 16), 21552, 22));
10: coll.Add(new Cleanup(IndexusMessage.CacheItemPriority.Low, new TimeSpan(0, 9, 7), 5, new DateTime(2007, 12, 28, 14, 12, 17), 21252, 1));
11: coll.Add(new Cleanup(IndexusMessage.CacheItemPriority.NotRemovable, new TimeSpan(0, 22, 7), 6, new DateTime(2007, 12, 28, 14, 12, 19), 212332, 5));
12: coll.Add(new Cleanup(IndexusMessage.CacheItemPriority.AboveNormal, new TimeSpan(0, 41, 7), 7, new DateTime(2007, 12, 28, 14, 12, 22), 211232, 55));
13: coll.Add(new Cleanup(IndexusMessage.CacheItemPriority.BelowNormal, new TimeSpan(0, 58, 7), 8, new DateTime(2007, 12, 28, 14, 12, 21), 22532, 25));
14:
15: Console.WriteLine(@"Without Sorting");
16: foreach (Cleanup c in coll)
17: {
18: Console.WriteLine(c.ToString());
19: }
20:
21: Cleanup.Sorting = Cleanup.SortingOrder.Asc;
22: coll.Sort(Cleanup.CacheItemPriority);
23: Console.WriteLine(@"Normal Sorting");
24: foreach (Cleanup c in coll)
25: {
26: Console.WriteLine(c.ToString());
27: }
28:
29: Cleanup.Sorting = Cleanup.SortingOrder.Desc;
30: coll.Sort(Cleanup.CacheItemPriority);
31: Console.WriteLine(@"Reverse Sorting");
32: foreach (Cleanup c in coll)
33: {
34: Console.WriteLine(c.ToString());
35: }
36: }
37: }
it's couldn't be easier :-)
Saturday, December 22, 2007
a simple und strightforward way to sort generic List
at 7:51 PM Posted by roni schuetz
Labels: .net, cache, development, memory, server, SharedCache
Subscribe to:
Post Comments (Atom)
Shared Cache - .Net Caching made easy
All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.
No comments:
Post a Comment