代码之家  ›  专栏  ›  技术社区  ›  Codejoy

在c#中使用多个字段对列表框进行排序

  •  0
  • Codejoy  · 技术社区  · 6 年前

    有没有一种方法可以先对一个字段的列表框排序,然后再对另一个字段排序?

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

    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 没有帮助,我相信可能会有一个表情(林克来拯救?)。我正准备带着一大堆箱子走很长一段路的时候 studiesAndFolders 把它列在名单上。

    4 回复  |  直到 6 年前
        1
  •  0
  •   Zer0    6 年前

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

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

    正如其他人所指出的,您应该存储 StudyDate DateTime 除非你想按字母顺序排列。

        2
  •  0
  •   Jack Sparrow    6 年前

    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
  •  0
  •   Fabio    6 年前

    根据您需要的日期正确订购 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);
    }
    

    .ToString() 中的方法 MyListBoxItem

    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}";
        }
    }    
    
        4
  •  -1
  •   Ä°lker Çakır    6 年前

    MyList.OrderBy(x => x.StudyName).ThenByDescending(x=>x.StudyDate).ToList()