【问题标题】:How to sort list on base of checkBoxes如何根据复选框对列表进行排序
【发布时间】:2017-03-29 23:29:55
【问题描述】:

我想根据检查和控件的未选中状态对数组列表进行排序,选中的复选框将排在第一位,未选中的复选框将排在列表的后面。然后我会将它添加到面板中。这怎么可能?

【问题讨论】:

    标签: c# sorting


    【解决方案1】:

    将 CheckBoxes 放在一个通用列表中并使用其Sort 方法。

    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);
    

    【讨论】:

    • 我认为checkBox =&gt; return checkBox.Checked ? -1 : +1 (-1 和 +1 放错了位置,这将排序未选中然后选中)问题是在未选中之前将所有选中排序。
    • 我正面临这个错误 ** 错误 13 委托 'System.Comparison' 不采用 '1' 参数 **
    • 抱歉,比较委托需要两个参数。编辑了我的答案。
    【解决方案2】:

    您可以改用一个通用的复选框列表并按如下方式对其进行排序:

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

    确保初始化列表...

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 2014-03-24
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 2020-06-02
      • 2014-12-29
      • 1970-01-01
      相关资源
      最近更新 更多