Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Miroslav Stampar http://mstampar.awardspace.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Sort generic list

   1  
   2  public class Item
   3  {
   4   public Item(string term, int freq)
   5   {
   6    _term = term;
   7    _freq = freq;
   8   }
   9  
  10   private string _term;
  11   public string Term
  12   {
  13    get { return _term; }
  14    set { _term = value; }
  15   }
  16  
  17    private int _freq;
  18    public int Freq
  19    {
  20     get { return _freq; }
  21     set { _freq = value; }
  22    }
  23   }
  24  
  25   public class ItemComparer:IComparer<Item>
  26   {
  27    #region IComparer<Item> Members
  28  
  29    public int Compare(Item x, Item y)
  30    {
  31     return y.Freq - x.Freq; //descending sort
  32     //return x.Freq - y.Freq; //ascending sort
  33    }
  34  
  35    #endregion
  36   }
  37  
  38  static void Main()
  39  {
  40   List<Item> items = new List<Item>();
  41   ....
  42   items.Sort(new ItemComparer());
  43  }
  44  
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS