首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OrderBy on List<string>性质

OrderBy on List<string>性质
EN

Stack Overflow用户
提问于 2015-06-01 20:13:21
回答 1查看 125关注 0票数 0

如何以下列方式对列表类型属性执行OrderBy?

我想按名字和列表中的第一个字符串来订购

UnOrdered : ResearcherNames

  • 佐伊·拉斯,乔·史密斯
  • 凯尔·伯特
  • 亚当·郑

排序: ResearcherNames

  • 亚当·郑
  • 凯尔·伯特
  • 佐伊·拉斯,乔·史密斯

我的课是这样的

代码语言:javascript
复制
public class assignments
{
    public List<string> ReseacherNames { get; set; }
    public List<string> ClientUserNames { get; set; }
    ------Other Properties ------
}

 //fill list
 combinedAssignments = new List<assignments>();
 foreach(var item in db) 
 {
    combinedAssingments.Add(item)
 }

 //need to sort by ResearcherNames
 combinedAssignments = combinedAssignments.OrderBy(x => x.ReseacherNames ).ToList();

当我使用上面的OrderBy调用运行时,我得到“至少有一个对象必须实现IComparable”。错误。

尝试

因此,我意识到我必须对列表类型使用IComparer,并实现了一个比较器,如下所示:

代码语言:javascript
复制
 public class RecordComparer : IComparer<List<string>>
 {
        public int Compare(string x, string y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're 
                    // equal.  
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y 
                    // is greater.  
                    return -1;
                }
            }
            else
            {
                // If x is not null... 
                // 
                if (y == null)
                // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    // ...and y is not null, compare the  
                    // lengths of the two strings. 
                    // 
                    int retval = x.Length.CompareTo(y.Length);

                    if (retval != 0)
                    {
                        // If the strings are not of equal length, 
                        // the longer string is greater. 
                        // 
                        return retval;
                    }
                    else
                    {
                        // If the strings are of equal length, 
                        // sort them with ordinary string comparison. 
                        // 
                        return x.CompareTo(y);
                    }
                }
            }
        }

        public int Compare(List<string> x, List<string> y)
        {
            var result = 0;
            if ((x != null && x.Count>0 )&& ( y != null && y.Count>0) )
            {

                return Compare(x[0], y[0]);
            }

            return result;
        }
    }
}

用法:

代码语言:javascript
复制
var rc = new RecordComparer();
 combinedAssignments = combinedAssignments.OrderBy(x=>     x.ResearcherNames,rc).ToList();

没有错误消息,但是结果没有alpha排序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-02 00:10:01

不需要IComparer。这将按照第一位研究员的名字给你分配任务:

代码语言:javascript
复制
combinedAssignments = combinedAssignments
  .OrderBy(x => x.ReseacherNames.FirstOrDefault())
  .ToList();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30582419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档