Advertisement
Guest User

Untitled

a guest
Aug 27th, 2012
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1.     namespace testcase
  2.     {
  3.         public class Program
  4.         {
  5.             static void Main(string[] args)
  6.             {
  7.                 var vote1 = new Vote() { ItemID = 1 };
  8.                 List<Vote> votes = new List<Vote>();
  9.                 votes.Add(vote1);
  10.  
  11.                 List<string> strings = new List<string>();
  12.                 strings.Add("test");
  13.                 List<Post> posts = Post.SortedCollection<Post>(strings, ListSortType.Hot, votes);
  14.             }
  15.         }
  16.  
  17.         public interface IVotable
  18.         {
  19.             double HotScore { get; set; }
  20.             double VoteTotal { get; set; }
  21.             DateTime CreatedDate { get; set; }
  22.         }
  23.         public class SCO : IVotable
  24.         {
  25.             public double HotScore { get; set; }
  26.             public double VoteTotal { get; set; }
  27.             public DateTime CreatedDate { get; set; }
  28.  
  29.             public SCO(string item, List<Vote> votes)
  30.             {
  31.                 VoteTotal = 10;
  32.                 HotScore = 10;
  33.                 CreatedDate = DateTime.Now;
  34.             }
  35.  
  36.             public static List<T> SortedCollection<T>(List<string> items, ListSortType sortType, List<Vote> votes) where T : IVotable
  37.             {
  38.                 var returnlist = new List<T>();
  39.                 Type genericType = typeof(T);
  40.                 var functionCreator = FastActivator.GenerateFunc<Func<string, List<Vote>, T>>();
  41.  
  42.                 for (int i = 0; i < items.Count; i++) { returnlist.Add(functionCreator(items[i], votes)); }
  43.                 switch (sortType)
  44.                 {
  45.                     case ListSortType.Hot:
  46.                         returnlist.Sort((p1, p2) => p2.HotScore.CompareTo(p1.HotScore));
  47.                         break;
  48.                     case ListSortType.Top:
  49.                         returnlist.Sort((p1, p2) => p2.VoteTotal.CompareTo(p1.VoteTotal));
  50.                         break;
  51.                     case ListSortType.Recent:
  52.                         returnlist.Sort((p1, p2) => p2.CreatedDate.CompareTo(p1.CreatedDate));
  53.                         break;
  54.                 }
  55.                 return returnlist;
  56.             }
  57.         }
  58.  
  59.         public class Vote
  60.         {
  61.             public double ItemID { get; set; }
  62.         }
  63.         public class VoteMeta
  64.         {
  65.             public double UpVotes { get; set; }
  66.             public double DownVotes { get; set; }
  67.             public string CurrentUserVoteClass { get; set; }
  68.         }
  69.         public enum ListSortType { Hot, Top, Recent };
  70.  
  71.         public class Post : SCO
  72.         {
  73.             public string Summary { get; set; }
  74.             public Uri Link { get; set; }
  75.  
  76.             public Post(string item, List<Vote> votes)
  77.                 : base(item, votes)
  78.             {
  79.                 Summary = "Summary";
  80.                 Link = new UriBuilder("http://www.google.com").Uri;
  81.             }
  82.         }
  83.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement