TagSort.cs

00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using Shared;
00005 
00006 namespace TagControls
00007 {
00011     class Sorter
00012     {
00017         public static void AlphabeticalSortTags(List<TagInfo> tags) 
00018         {
00019             int i, min;
00020             
00021             //tmp list used to sort the tags in
00022             List<TagInfo> tmpTagList = new List<TagInfo>();
00023 
00024             // - > O(n^2)
00025             // this can be done better but this is easy.
00026             while (tags.Count > 0)
00027             {
00028                 min = 0;
00029                 for (i = 0; i < tags.Count; i++)
00030                 {
00031                     if (tags[min].Name.CompareTo(tags[i].Name) >= 0)
00032                         min = i;
00033                 }
00034                 tmpTagList.Add(tags[min]);
00035                 tags.RemoveAt(min);
00036             }
00037 
00038             foreach (TagInfo tag in tmpTagList)
00039                 tags.Add(tag);
00040         }
00041 
00046         public static void WeightSortTags(List<TagsInfo> tags)
00047         {
00048             int i, min;
00049            
00050             List<TagsInfo> tmpTagList = new List<TagsInfo>();
00051 
00052             // - > O(n^2)
00053             // this can be done better but this is easy.
00054             while (tags.Count > 0)
00055             {
00056                 min = 0;
00057                 for (i = 0; i < tags.Count; i++)
00058                 {
00059                     if (tags[min].Weight <  tags[i].Weight)
00060                         min = i;
00061                 }
00062                 tmpTagList.Add(tags[min]);
00063                 tags.RemoveAt(min);
00064             }
00065 
00066             foreach (TagsInfo tag in tmpTagList)
00067                 tags.Add(tag);
00068         }
00069 
00070 
00077         public static void WeightAndGroupTags(List<TagInfo> tags, List<TagsInfo> updatedTagList, string username)
00078         {
00079             int i, MaxWeight = 1;
00080             bool mytag;
00081 
00082             TagsInfo tmpTag;
00083 
00084             updatedTagList.Clear();
00085             //remove al tags there occur more than one time
00086             for (i = 0; i <= tags.Count - 1; i++)
00087             {
00088                 if (username == tags[i].UserName)
00089                     mytag = true;
00090                 else
00091                     mytag = false;
00092 
00093                 //becaus the input list is sorted Alphabetical we can do this 
00094                 if (i != 0 && updatedTagList[updatedTagList.Count - 1].Name.ToLower() == tags[i].Name.ToLower())
00095                 {
00096                     updatedTagList[updatedTagList.Count - 1].Weight += 1;
00097                     updatedTagList[updatedTagList.Count - 1].Name = tags[i].Name; //case hax! :D cool!
00098 
00099                     if (tags[i].UserName == username)
00100                         updatedTagList[updatedTagList.Count - 1].myTag = true;
00101 
00102                     if (updatedTagList[updatedTagList.Count - 1].Weight > MaxWeight)
00103                         MaxWeight = updatedTagList[updatedTagList.Count - 1].Weight;
00104                 }
00105                 else
00106                 {
00107                     tmpTag = new TagsInfo(tags[i].Name, 1, mytag, tags[i].ID);
00108                     updatedTagList.Add(tmpTag);
00109                 }
00110             }
00111 
00112             //calculat the weight of the tag in %
00113             foreach (TagsInfo Tag in updatedTagList)
00114             {
00115                 Tag.Weight = Convert.ToInt32((100.0 / Convert.ToDouble(MaxWeight)) + 1) * Tag.Weight;
00116                 if (Tag.Weight > 100)
00117                     Tag.Weight = 100;
00118             }
00119         }
00120     }
00121 
00125     public class TagsInfo
00126     {
00127         public string Name;
00128         public int Weight, Id;
00129         public bool myTag;
00130 
00131         public TagsInfo(string name, int weight, bool myTag, int ID)
00132         {
00133             this.Name = name;
00134             this.Weight = weight;
00135             this.myTag = myTag;
00136             this.Id = ID;
00137         }
00138     }
00139 }

Generated on Thu Dec 21 06:21:57 2006 for SCRAML by  doxygen 1.5.1-p1