views:

50

answers:

3

I have two checkboxes (Recommended and Others) which have peoples names (concatenated, i.e. John Smith is one item). I want to alphabetize the selected members of each list into one. How can I do this?

A: 

You can put the selected members of both into a List of strings and sort, then put the list of strings into a new CheckBoxList.

See http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx for the MSDN List(T).Sort Method example.

Michael Wheeler
+1  A: 

An ASP.NET implementation with three checkboxlist controls (chkRecommended, chkOthers, chkCombined)

var listItems = (from ListItem listItem in chkRecommended.Items
                 where listItem.Selected
                 select listItem)
                .Union(from ListItem listItem in chkOthers.Items
                       where listItem.Selected
                       select listItem)
                .OrderBy(listItem => listItem.Text);

chkCombined.Items.Clear();
foreach (ListItem listItem in listItems)
    chkCombined.Items.Add(listItem);

If you just meant a list of the values rather than another control, you can modify the original query I provided or extend it like so

var listValues = listItems.Select(listItem => listItem.Value);
Anthony Pegram
A: 

If you're grabbing them from a SqlDataSource, why not do the alphabetizing via SQL?

Jason M
Doesn't he want to alphabetize them after he has gotten them from SQL, once they are selected?
Michael Wheeler
@Michael Wheeler, that is correct.
Matthew Jones