Listbox Sorting

  • Thread starter Thread starter 00_ChInkPoIntD12
  • Start date Start date
0

00_ChInkPoIntD12

Can anyone confirm there isn't a Sort() method for WebControl Listbox in
Asp.net?

It is rather simple to write a method to do the sorting, but just wondering
I shouldn't invent the wheel if there is already one.

C.P.
 
You should alway sort the data before putting it into a control. If you are
used DataSet's use the DataSet sort method. If you are using an array use
the array sort method.
 
There might be a way to have the underlying data source sort itself
depending on what kind of a data structure you're using.
 
may not work in my case, because I let customers to add their own item to
the Listbox and also picking existing pre-defined items. How would you
suggest a good way to handle this? Thanks in advance..
 
Okay.. that means there isn't one. I just spent some time to write one.
Hope this can help other people. Assuming you have the ListBox defined as
"uiProductListBox" in your aspx page, here is how to use it: (Note: you
may have to define CurrentCulture in your global.asax)

How to use?
--------------------------------------------------------------
Listbox.Sort(ref uiProductListBox, Listbox.SortOrder.Descending);

--------------------------------------------------------------

public class Listbox
{
public enum SortOrder
{
Ascending, Descending
};

private class Reverser : IComparer
{
// You can use CaseInsensitiveComparer.Compare if you want Non-case
sensitive comparison
int IComparer.Compare( Object x, Object y )
{
return( (new Comparer(Thread.CurrentThread.CurrentCulture)).Compare( y,
x ) );
}

}

public static void Sort(ref System.Web.UI.WebControls.ListBox myListBox,
SortOrder mySortOrder)
{

SortedList myList;
IComparer myComparer = new Reverser();

if (mySortOrder == SortOrder.Ascending)
myList = new SortedList();
else
myList = new SortedList(myComparer);

for (int i=0; i< myListBox.Items.Count; i++)
myList.Add(myListBox.Items.Text.Trim(),
myListBox.Items.Value.Trim());

myListBox.Items.Clear();

for (int i=0; i< myList.Count; i++)
myListBox.Items.Add(new
System.Web.UI.WebControls.ListItem(myList.GetKey(i).ToString().Trim(),
myList.GetByIndex(i).ToString().Trim()));


}
}




C.P.
 
The list box items is an array, all arrays have a sort function. i.e.
ListBox.Items.Sort(); This would of done the same thing.

If I was writing the code, I would keep an array of the items. then use this
array as the data source of the list control (Data Binding). When a user
adds an item I would add it to my array. The list control would then display
the new item since the array is the data source of the list control. I would
sort the array when ever it was changed (items added or deleted). The array
could be an array of strings or an array of objects. If it is an array of
string there is a class for these in the collection library. simply calling
the array's sort function would preform the same functionality.

00_ChInkPoIntD12 said:
Okay.. that means there isn't one. I just spent some time to write one.
Hope this can help other people. Assuming you have the ListBox defined as
"uiProductListBox" in your aspx page, here is how to use it: (Note: you
may have to define CurrentCulture in your global.asax)

How to use?
--------------------------------------------------------------
Listbox.Sort(ref uiProductListBox, Listbox.SortOrder.Descending);

--------------------------------------------------------------

public class Listbox
{
public enum SortOrder
{
Ascending, Descending
};

private class Reverser : IComparer
{
// You can use CaseInsensitiveComparer.Compare if you want Non-case
sensitive comparison
int IComparer.Compare( Object x, Object y )
{
return( (new Comparer(Thread.CurrentThread.CurrentCulture)).Compare( y,
x ) );
}

}

public static void Sort(ref System.Web.UI.WebControls.ListBox myListBox,
SortOrder mySortOrder)
{

SortedList myList;
IComparer myComparer = new Reverser();

if (mySortOrder == SortOrder.Ascending)
myList = new SortedList();
else
myList = new SortedList(myComparer);

for (int i=0; i< myListBox.Items.Count; i++)
myList.Add(myListBox.Items.Text.Trim(),
myListBox.Items.Value.Trim());

myListBox.Items.Clear();

for (int i=0; i< myList.Count; i++)
myListBox.Items.Add(new
System.Web.UI.WebControls.ListItem(myList.GetKey(i).ToString().Trim(),
myList.GetByIndex(i).ToString().Trim()));


}
}




C.P.





00_ChInkPoIntD12 said:
may not work in my case, because I let customers to add their own item to
the Listbox and also picking existing pre-defined items. How would you
suggest a good way to handle this? Thanks in advance..
 
ListBox.Items is ListItemCollection, I don't see a Sort ()
function...http://msdn.microsoft.com/library/default.asp?url=/library/en-us/
cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionclasstopic.asp

please correct me if I am wrong.

C.P.


jfleeson said:
The list box items is an array, all arrays have a sort function. i.e.
ListBox.Items.Sort(); This would of done the same thing.

If I was writing the code, I would keep an array of the items. then use this
array as the data source of the list control (Data Binding). When a user
adds an item I would add it to my array. The list control would then display
the new item since the array is the data source of the list control. I would
sort the array when ever it was changed (items added or deleted). The array
could be an array of strings or an array of objects. If it is an array of
string there is a class for these in the collection library. simply calling
the array's sort function would preform the same functionality.

00_ChInkPoIntD12 said:
Okay.. that means there isn't one. I just spent some time to write one.
Hope this can help other people. Assuming you have the ListBox defined as
"uiProductListBox" in your aspx page, here is how to use it: (Note: you
may have to define CurrentCulture in your global.asax)

How to use?
--------------------------------------------------------------
Listbox.Sort(ref uiProductListBox, Listbox.SortOrder.Descending);

--------------------------------------------------------------

public class Listbox
{
public enum SortOrder
{
Ascending, Descending
};

private class Reverser : IComparer
{
// You can use CaseInsensitiveComparer.Compare if you want Non-case
sensitive comparison
int IComparer.Compare( Object x, Object y )
{
return( (new Comparer(Thread.CurrentThread.CurrentCulture)).Compare( y,
x ) );
}

}

public static void Sort(ref System.Web.UI.WebControls.ListBox myListBox,
SortOrder mySortOrder)
{

SortedList myList;
IComparer myComparer = new Reverser();

if (mySortOrder == SortOrder.Ascending)
myList = new SortedList();
else
myList = new SortedList(myComparer);

for (int i=0; i< myListBox.Items.Count; i++)
myList.Add(myListBox.Items.Text.Trim(),
myListBox.Items.Value.Trim());

myListBox.Items.Clear();

for (int i=0; i< myList.Count; i++)
myListBox.Items.Add(new
System.Web.UI.WebControls.ListItem(myList.GetKey(i).ToString().Trim(),
myList.GetByIndex(i).ToString().Trim()));


}
}




C.P.





00_ChInkPoIntD12 said:
may not work in my case, because I let customers to add their own item to
the Listbox and also picking existing pre-defined items. How would you
suggest a good way to handle this? Thanks in advance..


You should alway sort the data before putting it into a control. If you
are
used DataSet's use the DataSet sort method. If you are using an
array
use
the array sort method.

Can anyone confirm there isn't a Sort() method for WebControl Listbox
in
Asp.net?

It is rather simple to write a method to do the sorting, but just
wondering
I shouldn't invent the wheel if there is already one.

C.P.
 
I'm sorry, your right ListItemCollection doesn't had a sort command. Above
would be another way to do the sort:

public class myListItemComparer : IComparer
{
int IComparer.Compare(object x, object y )
{
ListItem item_1 = x as ListItem;
ListItem item_2 = y as ListItem;

return item_1.Value.CompareTo(item_2.Value);
}
}

ListItemCollection c = ListBox1.Items;
ListItem[] items = new ListItem[c.Count];

c.CopyTo(items, 0);
System.Array.Sort(items, new myListItemComparer());

ListBox1.Items.Clear();
ListBox1.Items.AddRange(items);



00_DotNetWarrior said:
ListBox.Items is ListItemCollection, I don't see a Sort ()
function...http://msdn.microsoft.com/library/default.asp?url=/library/en-us/
cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionclasstopic.asp

please correct me if I am wrong.

C.P.


jfleeson said:
The list box items is an array, all arrays have a sort function. i.e.
ListBox.Items.Sort(); This would of done the same thing.

If I was writing the code, I would keep an array of the items. then use this
array as the data source of the list control (Data Binding). When a user
adds an item I would add it to my array. The list control would then display
the new item since the array is the data source of the list control. I would
sort the array when ever it was changed (items added or deleted). The array
could be an array of strings or an array of objects. If it is an array of
string there is a class for these in the collection library. simply calling
the array's sort function would preform the same functionality.

00_ChInkPoIntD12 said:
Okay.. that means there isn't one. I just spent some time to write one.
Hope this can help other people. Assuming you have the ListBox defined as
"uiProductListBox" in your aspx page, here is how to use it: (Note: you
may have to define CurrentCulture in your global.asax)

How to use?
--------------------------------------------------------------
Listbox.Sort(ref uiProductListBox, Listbox.SortOrder.Descending);

--------------------------------------------------------------

public class Listbox
{
public enum SortOrder
{
Ascending, Descending
};

private class Reverser : IComparer
{
// You can use CaseInsensitiveComparer.Compare if you want Non-case
sensitive comparison
int IComparer.Compare( Object x, Object y )
{
return( (new Comparer(Thread.CurrentThread.CurrentCulture)).Compare( y,
x ) );
}

}

public static void Sort(ref System.Web.UI.WebControls.ListBox myListBox,
SortOrder mySortOrder)
{

SortedList myList;
IComparer myComparer = new Reverser();

if (mySortOrder == SortOrder.Ascending)
myList = new SortedList();
else
myList = new SortedList(myComparer);

for (int i=0; i< myListBox.Items.Count; i++)
myList.Add(myListBox.Items.Text.Trim(),
myListBox.Items.Value.Trim());

myListBox.Items.Clear();

for (int i=0; i< myList.Count; i++)
myListBox.Items.Add(new
System.Web.UI.WebControls.ListItem(myList.GetKey(i).ToString().Trim(),
myList.GetByIndex(i).ToString().Trim()));


}
}




C.P.






may not work in my case, because I let customers to add their own item to
the Listbox and also picking existing pre-defined items. How would you
suggest a good way to handle this? Thanks in advance..


You should alway sort the data before putting it into a control. If you
are
used DataSet's use the DataSet sort method. If you are using an array
use
the array sort method.

Can anyone confirm there isn't a Sort() method for WebControl Listbox
in
Asp.net?

It is rather simple to write a method to do the sorting, but just
wondering
I shouldn't invent the wheel if there is already one.

C.P.

 
Back
Top