|
 |
|
|
I put your code to use in a User Control, then registered the control in the web.config file so I can add it directly to any page like so:
<MyTag:SortableList ID="MyList" runat="server" ListType="DropDownList" />
The user control simply adds whatever type of list control you need and provides the means to sort the items by Text or by Value, ascending or descending. There's also a method to add an item to the top of the list (AddFirstItem) if you want to add a default item to the top of the list after you sort the items.
Just add a new User Control to your project and put the code below in the codebehind - no other controls are necessary unless you want to do more with it.
Partial Class Controls_SortableListControl Inherits System.Web.UI.UserControl
Public Enum ListTypes DropDownList ListBox RadioButtonList CheckBoxList BulletedList End Enum
Private _ListType As ListTypes Public Property ListType() As ListTypes Get If _ListType = Nothing Then _ListType = ListTypes.DropDownList End If Return _ListType End Get Set(ByVal value As ListTypes) _ListType = value ListSetup() End Set End Property
Public Property ListControl() As Object Get If Me.FindControl("ListControl") Is Nothing Then ListSetup() End If Return Me.FindControl("ListControl") End Get Private Set(ByVal value As Object) If value IsNot Nothing Then If Me.FindControl("ListControl") Is Nothing Then value.ID = "ListControl" Me.Controls.Add(value) End If End If End Set End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub ListSetup() Select Case ListType Case ListTypes.BulletedList ListControl = New BulletedList Case ListTypes.CheckBoxList ListControl = New CheckBoxList Case ListTypes.DropDownList ListControl = New DropDownList Case ListTypes.ListBox ListControl = New ListBox Case ListTypes.RadioButtonList ListControl = New RadioButtonList Case Else ListControl = Nothing End Select End Sub
Public Sub AddFirstItem(ByVal li As ListItem) ListControl.Items.Insert(0, li) End Sub
Public Sub SortList(ByVal SortByText As Boolean, ByVal SortAscending As Boolean) Dim ar(ListControl.Items.Count - 1) As ListItem ListControl.Items.CopyTo(ar, 0)
If SortByText Then Array.Sort(ar, New ListItemCompareText) Else Array.Sort(ar, New ListItemCompareValue) End If
If Not SortAscending Then Array.Reverse(ar) End If
ListControl.Items.Clear() ListControl.Items.AddRange(ar) End Sub
Private Class ListItemCompareText Implements IComparer
Public Function Compare(ByVal x As Object, _ ByVal y As Object) As Integer _ Implements System.Collections.IComparer.Compare Dim a As ListItem = x Dim b As ListItem = y Dim c As New CaseInsensitiveComparer Return c.Compare(a.Text, b.Text) End Function End Class Private Class ListItemCompareValue Implements IComparer
Public Function Compare(ByVal x As Object, _ ByVal y As Object) As Integer _ Implements System.Collections.IComparer.Compare Dim a As ListItem = x Dim b As ListItem = y Dim c As New CaseInsensitiveComparer Return c.Compare(a.Value, b.Value) End Function End Class End Class
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm having problem with this code...
Private Class ListItemComparer _ Implements IComparer Public Function Compare(ByVal x As Object, _ ByVal y As Object) As Integer _ Implements System.Collections.IComparer.Compare
Is the "IComparer" a reference?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
put imports System.Collections at the top of your file before the class declaration. IComarer is part of this namespace. You could alternatively use the fully qualified name of: Private Class ListItemCompater Implements System.Collections.IComparer
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Still not working to me....kindly please send me a sample project for this i possible. Thank you in advance.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm sorry, but I don't have time to do that. As you can see from the posts below, this code should work as is in a VB ASP.NET application. There's truely not much to it.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
the same problem is happening to me. I couldn't find IComparer in Collections Namespace. Only Comparer is there. I use VS 2003 and .NET framework 1.1. Is there something wrong or not applicable yet?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
The error is here : Private Class ListItemComparer _ Implements IComparer
You must remove the underscore "_" from the end of line
Muhammad
Muhammad Elnahrawi www.mycodebank.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks for the idea Domenic. I made a slight tweak.
Private Sub SortDropDown(ByVal dd As DropDownList) Dim ar(dd.Items.Count - 1) As ListItem dd.Items.CopyTo(ar, 0) dd.Items.Clear() ar.Sort(ar, New ListItemComparer) dd.Items.AddRange(ar) End Sub
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Nice code. It worked immediately. However, when you have an item like "Select one" and want to have it displayed as the default selected item, it will fall down the list of items in the dropdown list. So to make this work, I added on line to your code to add "Select one" item to the top of the list after the sort. Here is your code with the one extra line of code I added:
Private Sub SortDropDown(ByVal dd As DropDownList) Dim ar As ListItem() Dim i As Long = 0 For Each li As ListItem In dd.Items ReDim Preserve ar(i) ar(i) = li i += 1 Next Dim ar1 As Array = ar
ar1.Sort(ar1, New ListItemComparer) dd.Items.Clear() 'Add an item that will appear as the default selected item in the dropdown list dd.Items.Add(New ListItem("Select one", "Select one")) dd.Items.AddRange(ar1) End Sub
Private Class ListItemComparer Implements IComparer
Public Function Compare(ByVal x As Object, _ ByVal y As Object) As Integer _ Implements System.Collections.IComparer.Compare Dim a As ListItem = x Dim b As ListItem = y Dim c As New CaseInsensitiveComparer Return c.Compare(a.Text, b.Text) End Function End Class
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
What balls programming.
Whoever wrote this should really work for the framework development team... their programming is little better.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Just wanted to let you know there's an error when i compile this code.
This code work perfectly when the "_" sign is removed in the Class declaration.
Public Class ListItemComparer Implements IComparer _
Thanks for the tip.
Jean-Francois Chatelain
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Thanks for the sample. I needed this in c#, so I wrapped it in a SortableDropDownList
public class SortableDropDownList : DropDownList { class ListItemComparer : IComparer{ public int Compare(object a, object b){ return new CaseInsensitiveComparer().Compare(((ListItem)a).Text, ((ListItem)b).Text); } } public void SortItems(){ ListItem[] items = new ListItem[Items.Count]; int i = 0; foreach(ListItem item in Items) items[i++] = item; Array.Sort(items, new ListItemComparer()); Items.Clear(); Items.AddRange(items); } }
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
Your code returns a null array of listitems, here it the correct version
public void SortDropDown(ref DropDownList ddl) { ListItem[] liArray = new ListItem[ddl.Items.Count]; ddl.Items.CopyTo(liArray, 0); Array.Sort(liArray, new ListItemComparer()); ddl.Items.Clear(); ddl.Items.AddRange(liArray ); }
public class ListItemComparer : IComparer { public int Compare(object a, object b) { return new CaseInsensitiveComparer().Compare(((ListItem)a).Text, ((ListItem)b).Text); } }
|
| Sign In·View Thread·PermaLink | 4.50/5 (4 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
I try to improve on the SortMethod.
public void SortListItem(DropDownList dropDownList, Enum sortType) { ListItem[] listItem = new ListItem[dropDownList.Items.Count]; dropDownList.Items.CopyTo(listItem, 0); if (sortType.ToString().Equals("Text")) { Array.Sort(listItem, new ListItemTextComparer()); } else { Array.Sort(listItem, new ListItemValueComparer()); }
dropDownList.Items.Clear(); dropDownList.Items.AddRange(listItem); }
internal class ListItemTextComparer : IComparer { public int Compare(object x, object y) { return new CaseInsensitiveComparer().Compare((x as ListItem).Text, (y as ListItem).Text); } }
internal class ListItemValueComparer : IComparer { public int Compare(object x, object y) { return new CaseInsensitiveComparer().Compare((x as ListItem).Value, (y as ListItem).Value); } }
my Blog : ruilei.cnblogs.com
fgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfgfgfdgfdgdfgdfgfdgdfgdfg
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
ListItem's Forecolor is "Black" I want turn the forecolor of one of ListItems in the DropDownList to "Red",and others are "Black", How can I do this?
|
| Sign In·View Thread·PermaLink | 3.00/5 (1 vote) |
|
|
|
 |
|
|
 |