Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old September 21st, 2005, 03:04 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,276 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 13 h 24 m 17 sec
Reputation Power: 1795
Post ASP.NET using C# - Sorting DropDownList by Text or Value

Two very simple methods for two kinds of sorting.
Declared static to let you put it inside seperate class called Tools
for example then using this whenever you need.

C# Code:
Original - C# Code
  1.  
  2. #region sorting methods
  3. public static void SortByValue(ListControl combo)
  4. {
  5.     SortCombo(combo, new ComboValueComparer());
  6. }
  7.  
  8. public static void SortByText(ListControl combo)
  9. {
  10.     SortCombo(combo, new ComboTextComparer());
  11. }
  12. #endregion
  13.  
  14. #region Private Methods - for internal use
  15. private static void SortCombo(ListControl combo, IComparer comparer)
  16. {
  17.     int i;
  18.     if (combo.Items.Count <= 1)
  19.         return;
  20.     ArrayList arrItems=new ArrayList();
  21.     for (i=0; i<combo.Items.Count; i++)
  22.     {
  23.         ListItem item=combo.Items[i];
  24.         arrItems.Add(item);
  25.     }
  26.     arrItems.Sort(comparer);
  27.     combo.Items.Clear();
  28.     for (i=0; i<arrItems.Count; i++)
  29.     {
  30.         combo.Items.Add((ListItem) arrItems[i]);
  31.     }
  32. }
  33.  
  34. #region Combo Comparers
  35. /// <summary>
  36. /// compare list items by their value
  37. /// </summary>
  38. private class ComboValueComparer : IComparer
  39. {
  40.     public enum SortOrder
  41.     {
  42.         Ascending=1,
  43.         Descending=-1
  44.     }
  45.  
  46.     private int _modifier;
  47.  
  48.     public ComboValueComparer()
  49.     {
  50.         _modifier = (int) SortOrder.Ascending;
  51.     }
  52.  
  53.     public ComboValueComparer(SortOrder order)
  54.     {
  55.         _modifier = (int) order;
  56.     }
  57.  
  58.     //sort by value
  59.     public int Compare(Object o1, Object o2)
  60.     {
  61.         ListItem cb1=(ListItem) o1;
  62.         ListItem cb2=(ListItem) o2;
  63.         return cb1.Value.CompareTo(cb2.Value)*_modifier;
  64.     }
  65. } //end class ComboValueComparer
  66.  
  67. /// <summary>
  68. /// compare list items by their text.
  69. /// </summary>
  70. private class ComboTextComparer : IComparer
  71. {
  72.     public enum SortOrder
  73.     {
  74.         Ascending=1,
  75.         Descending=-1
  76.     }
  77.  
  78.     private int _modifier;
  79.  
  80.     public ComboTextComparer()
  81.     {
  82.         _modifier = (int) SortOrder.Ascending;
  83.     }
  84.  
  85.     public ComboTextComparer(SortOrder order)
  86.     {
  87.         _modifier = (int) order;
  88.     }
  89.  
  90.     //sort by value
  91.     public int Compare(Object o1, Object o2)
  92.     {
  93.         ListItem cb1=(ListItem) o1;
  94.         ListItem cb2=(ListItem) o2;
  95.         return cb1.Text.CompareTo(cb2.Text)*_modifier;
  96.     }
  97. } //end class ComboTextComparer
  98. #endregion
  99. #endregion
  100.  

as you can see, you can also define descending or ascending
order for the comparers.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > ASP.NET using C# - Sorting DropDownList by Text or Value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT