By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

Listbox Sorting

P: n/a
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.
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

"00_ChInkPoIntD12" <00**************@CuMPeEWeAe.com> wrote in message
news:q8********************@rogers.com...
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.

Nov 19 '05 #2

P: n/a
There might be a way to have the underlying data source sort itself
depending on what kind of a data structure you're using.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"00_ChInkPoIntD12" <00**************@CuMPeEWeAe.com> wrote in message
news:q8********************@rogers.com...
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.

Nov 19 '05 #3

P: n/a

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..
"jfleeson" <ja***********@roseheartinn.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
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.

"00_ChInkPoIntD12" <00**************@CuMPeEWeAe.com> wrote in message
news:q8********************@rogers.com...
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.


Nov 19 '05 #4

P: n/a
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)).Com pare( 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[i].Text.Trim(),
myListBox.Items[i].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" <00**************@CuMPeEWeAe.com> wrote in message
news:Y-********************@rogers.com...

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..
"jfleeson" <ja***********@roseheartinn.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
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.

"00_ChInkPoIntD12" <00**************@CuMPeEWeAe.com> wrote in message
news:q8********************@rogers.com...
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.



Nov 19 '05 #5

P: n/a
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" wrote:
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)).Com pare( 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[i].Text.Trim(),
myListBox.Items[i].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" <00**************@CuMPeEWeAe.com> wrote in message
news:Y-********************@rogers.com...

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..
"jfleeson" <ja***********@roseheartinn.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
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.

"00_ChInkPoIntD12" <00**************@CuMPeEWeAe.com> wrote in message
news:q8********************@rogers.com...
> 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.
>
>



Nov 19 '05 #6

P: n/a
ListBox.Items is ListItemCollection, I don't see a Sort ()
function...http://msdn.microsoft.com/library/de...library/en-us/
cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionclass topic.asp

please correct me if I am wrong.

C.P.
"jfleeson" <jf******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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" wrote:
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)).Com pare( 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[i].Text.Trim(),
myListBox.Items[i].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" <00**************@CuMPeEWeAe.com> wrote in message
news:Y-********************@rogers.com...

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..
"jfleeson" <ja***********@roseheartinn.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
> 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.
>
> "00_ChInkPoIntD12" <00**************@CuMPeEWeAe.com> wrote in

message > news:q8********************@rogers.com...
> > 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.
> >
> >
>
>


Nov 19 '05 #7

P: n/a
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" wrote:
ListBox.Items is ListItemCollection, I don't see a Sort ()
function...http://msdn.microsoft.com/library/de...library/en-us/
cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionclass topic.asp

please correct me if I am wrong.

C.P.
"jfleeson" <jf******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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" wrote:
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)).Com pare( 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[i].Text.Trim(),
myListBox.Items[i].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" <00**************@CuMPeEWeAe.com> wrote in message
news:Y-********************@rogers.com...
>
> 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..
>
>
> "jfleeson" <ja***********@roseheartinn.com> wrote in message
> news:On**************@TK2MSFTNGP12.phx.gbl...
> > 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.
> >
> > "00_ChInkPoIntD12" <00**************@CuMPeEWeAe.com> wrote in message > > news:q8********************@rogers.com...
> > > 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.
> > >
> > >
> >
> >
>
>


Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.