Zain Ali
Zain Ali

Reputation: 15983

How to sort list on base of checkBoxes

I want to sort array List of check boxes on base of check and unchecked status of controls checked check boxes will come first and unchecked checked boxes will come later in list. Then I shall add this to panel. How is this possible?

Upvotes: 0

Views: 1226

Answers (2)

Florian Greinacher
Florian Greinacher

Reputation: 14784

Put the CheckBoxes in a generic list and use its Sort method.

List<CheckBox> checkBoxes = GetCheckBoxes();

// Unchecked CheckBoxes first
checkBoxes.Sort((firstCheckBox, secondCheckBox) => return firstCheckBox.Checked ? +1 : -1);

// Checked CheckBoxes first
checkBoxes.Sort((firstCheckBox, secondCheckBox) => return firstCheckBox.Checked ? -1 : +1);

Upvotes: 1

sajoshi
sajoshi

Reputation: 2763

You can instead use a generic List of checkbox and sort it like below:

List<CheckBox> ar;
        ar.Sort(c => c.Checked);

Make sure to initialize the list ...

Upvotes: 0

Related Questions