【问题标题】:Sorting a ListBox in c# with multiple fields在 C# 中使用多个字段对 ListBox 进行排序
【发布时间】:2018-06-26 06:36:06
【问题描述】:

有没有办法在一个字段上对列表框进行排序?

(我看过其他帖子,但我认为这涉及更多)。我可以做到这一点,但认为有一个更快的速记版本我只是不知道。

基本上这会读取其中所有文件夹的目录,格式为:

DATENAME

我从日期中解析出名称。我需要按名称组织这些然后按日期(第二个过滤器是什么让我绊倒)。

所以一个文件夹:

12012016TULLY
1202019LAVA
2202018LAVA
5162019CLOUD
5202020LAVA

看起来像

5162019CLOUD
2202018LAVA
1202019LAVA
5202020LAVA
12012016TULLY

这就是我所拥有的:

class MyListBoxItem
{
    public string StudyBaseFolder { get; set; }
    public string StudyName { get; set; }
    public string UserLastName { get; set; }
    public string StudyDate { get; set; }
}

List<MyListBoxItem> studiesAndFolders = new List<MyListBoxItem>();

//later int he code i build a list of studyNames (which is a path and I pasre the path here too)
foreach (string sn in studyName)
{
    //get user name

    String lastName = getLastName(sn);
    String theDate = getDate(sn);

    //can I organize this based on the LAST NAME THEN THE DATE
    studiesAndFolders.Add(new MyListBoxItem { StudyBaseFolder = path, StudyName = sn, UserLastName = lastName, StudyDate = theDate }); 

 }

然后我终于将它添加到列表框中。

listDirectories.Items.Clear();

//I do it this way so a double click on an item gets the object back and I can do things with it. 
foreach(MyListBoxItem direc in studiesAndFolders)
{
    listDirectories.Items.Add(direc);
}

listbox.sorted=true 没有帮助,我确信可能有一个表达式(LINQ 来救援?)。当我把studiesAndFolders 放到列表中时,我正打算用大量的案例来做这件事。

【问题讨论】:

  • 你使用的是什么 UI 框架?
  • @Freggar Visual Studio 和标准 Windows UI(忘记它叫什么了。Windows 窗体?桌面应用程序)
  • 为什么不使用 LINQ?
  • 为什么不在添加到ListBox 之前简单地进行排序?

标签: c# list listbox


【解决方案1】:

此代码按名称排序,然后按日期排序。并且应该易于阅读。

foreach(MyListBoxItem direc in studiesAndFolders.OrderBy(x => x.StudyName).ThenBy(x => x.StudyDate))
{
    listDirectories.Items.Add(direc);
}

正如其他人所指出的,您应该将 StudyDate 存储为 DateTime,除非您希望它按字母顺序排序。

【讨论】:

    【解决方案2】:

    首先,将StudyDate的类型改为DateTime

    class MyListBoxItem
    {
        ...
        public DateTime StudyDate { get; set; }
    }
    

    之后,创建新的比较器

    public class MyListBoxItemComparer : IComparer<MyListBoxItem>
    {
        public int Compare(MyListBoxItem x, MyListBoxItem y)
        {
            if (x.StudyName == y.StudyName)
            {
                return x.StudyDate.CompareTo(y.StudyDate);
            }
    
            return String.Compare(x.StudyName, y.StudyName, StringComparison.Ordinal);
        }
    }
    

    最后,用SortedSet代替List

    SortedSet<MyListBoxItem> studiesAndFolders = new SortedSet<MyListBoxItem>(new MyListBoxItemComparer());
    

    【讨论】:

      【解决方案3】:

      要根据日期正确订购,您需要 StudyDate 的类型为 DateTime

      class MyListBoxItem
      {
          public string StudyBaseFolder { get; set; }
          public string StudyName { get; set; }
          public string UserLastName { get; set; }
          public DateTime StudyDate { get; set; }
      } 
      

      然后您可以使用 LINQ 扩展方法进行排序。

      var orderedDirectories = 
          directories.OrderBy(dir => dir.StudyName)
                     .ThenBy(dir => dir.StudyDate);
      
      foreach (var directory in orderedDirectories)
      {
          listBox.Items.Add(directory);
      }
      

      您可以覆盖MyListBoxItem 类中的.ToString() 方法,列表框将根据需要显示项目。

      class MyListBoxItem
      {
          public string StudyBaseFolder { get; set; }
          public string StudyName { get; set; }
          public string UserLastName { get; set; }
          public DateTime StudyDate { get; set; }
      
          public override string ToString()
          {
              return $"{StudyDate:MMddyyyy}{StudyName}";
          }
      }    
      

      【讨论】:

      • 我想你的意思是ThenBy 不是Then
      • @Zer0,谢谢修复。我们这里肯定需要智能感知;)
      • 哈,我希望。还认为您的意思是输入DateTime 而不是Date
      • 所以我用上面的目录结构测试了这些方法中的每一种,它似乎根本没有组织它。列表框以与它进入时相同的顺序获取它。执行时代码中没有错误。即使没有正确解析日期(我正在使用` DateTime.TryParseExact(theDate, "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date); ` 名称应该仍然但他们不是。挠头
      【解决方案4】:

      如果您可以在项目中使用 LINQ,首先您应该将 StudyDate 类型设为 DateTime。然后你可以这样做:

      MyList.OrderBy(x =&gt; x.StudyName).ThenByDescending(x=&gt;x.StudyDate).ToList()

      【讨论】:

        猜你喜欢
        • 2012-09-29
        • 1970-01-01
        • 1970-01-01
        • 2011-05-23
        • 2011-09-14
        • 2012-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多