使用多个字段在c#中对ListBox进行排序

时间:2018-06-26 06:36:07

标签: c# list listbox

有没有办法对一个字段然后对另一个字段进行排序?

(我看过其他文章,但我认为这涉及更多)。我可以做很长一段路,但是认为有一个我不知道的更快的速记版本。

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

DATENAME

我从日期中解析出名称。我需要按日期按名称THEN整理这些(第二个过滤条件是使我绊倒的原因)。

因此有一个文件夹:

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并将其放到列表中时,我将用大量案例做很长的路要走。

4 个答案:

答案 0 :(得分:0)

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

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

正如其他人指出的那样,除非您希望按字母顺序对StudyDate进行存储,否则应将DateTime存储为$scope.deleteTable = function($index){ // var i = $scope.users.indexOf(user); var myIndex = $index console.log(myIndex); var remove = confirm("Are you sure you want to delete!"); if(remove == true){ $scope.users.splice(myIndex,1) } } $scope.updateData = function(user){ console.log("index",index); $scope.users.push(index); }

答案 1 :(得分:0)

首先,将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());

答案 2 :(得分:0)

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

答案 3 :(得分:-1)

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

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