0

I have a foreach loop which fills a list with checkboxes from a groupbox. Problem is that they are added in a random order. Since I named them all in an ascending alphabetical order I want to sort them by their Name propery.

I've already tried using .Sort() but it doesn't do anything. Also I tried using a Linq expression DigBoxes = DigBoxes.OrderBy(x => x.Name).ToList();

But it also doesn't do anything...

Here's my code:

GroupBox box = (GroupBox)e.Argument;
string DigInput = "";
List<CheckBox> DigBoxes = new List<CheckBox>();

foreach (Control c in box.Controls)
{
    if(c is CheckBox)
    {
        DigBoxes.Add(c as CheckBox);
    }
}
DigBoxes = DigBoxes.OrderBy(x => x.Name).ToList();
Ziema
  • 45
  • 5
  • 2
    Define "doesn't do anything". – Chris Pickford Apr 18 '19 at 13:00
  • Where did you put the `.OrderBy`? – Jimenemex Apr 18 '19 at 13:01
  • Could you post code with the `.OrderBy` as you've implemented it and a sample of the output? Seems weird that that wouldn't work. – Broots Waymb Apr 18 '19 at 13:02
  • By saying doesn't do anything that is exatcly what I mean. After it executes that line of code nothing changes when I'm checking with debugger – Ziema Apr 18 '19 at 13:08
  • @Ziema I don't think you mean what you think you mean. Of course it's doing something, it's sorting the collection. You're just not using the result in a meaningful way. – Chris Pickford Apr 18 '19 at 13:11
  • I was thinking maybe my method doesn't apply to numbers. I've named my checkboxes like this: `CheckBox_Dig1_Value1`, `CheckBox_Dig1_Value2` etc. Can this be the problem? – Ziema Apr 18 '19 at 13:12
  • It's most likely that your UI is not updating correctly to use the `DigBoxes` collection – Jamie Rees Apr 18 '19 at 13:12
  • @Jamie Rees No, I'm not trying to change the UI, I just need the list to be sorted in an alphabetical order. – Ziema Apr 18 '19 at 13:15
  • @Aleks Andreev As I explained `foreach` adds all the checkboxes in a random order. So I'm trying to sort them by their `.Name` property. – Ziema Apr 18 '19 at 13:17

2 Answers2

2

I'm going off of some information you provided in a comment about numbers being in the name, so I may or may not be off here..
Depending on how many you have, you will definitely see a problem. Sorting by Name is a straight string comparison, that is, it doesn't take numerical values into account the same way.

For example, suppose your collection consists of CheckBox_7 and CheckBox_10. If you want those in numerical order, you'd expect 7 to come before 10. This isn't the case with strings. At index 9 of each name we have a 7 and a 1. 7 is obviously larger, which means as a whole, "CheckBox_7" is going to come after "CheckBox_10".

If you want to take the numerical values into account, you'll need to parse the name and add a little extra intelligence into a custom sort method.
Here's one question with something similar. It might be a good starting point for your specific case: sort string-numbers

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
  • 1
    Thank you! I removed the numeric values in the `Name` property and replaced them with alphabetical ones and this fixed the problem. – Ziema Apr 18 '19 at 13:32
0

Have you tried something like this:

var items = CheckBoxList1.Items
            .Cast<ListItem>()
            .OrderBy(i=>i.Text)
            .ToArray();
        CheckBoxList1.Items.Clear();
        CheckBoxList1.Items.AddRange(items);
johannesr
  • 21
  • 4