Skip to main content
 首页 » 编程设计

c#之在 C# 中对对象的 ArrayList 进行排序

2024年02月23日22cmt

如何对对象的 ArrayList 进行排序?我在对 ArrayList 进行排序时实现了 IComparable 接口(interface),但我没有得到所需的结果。

我的代码示例:

public class Sort : IComparable 
{ 
    public string Count { get; set; } 
    public string Url { get; set; } 
    public string Title { get; set; } 
 
    public int CompareTo(object obj) 
    { 
        Sort objCompare = (Sort)obj; 
        return (this.Count.CompareTo(objCompare.Count)); 
    } 
} 

这里我想根据Count对ArrayList进行排序。

请您参考如下方法:

试试这个:

public class Sort : IComparable<Sort> 
{ 
    public string Count { get; set; } 
    public string Url { get; set; } 
    public string Title { get; set; } 
 
    public virtual int CompareTo(Sort obj) 
    { 
        return (Count.CompareTo(obj.Count)); 
    } 
} 

由于 Count 是字符串,它可能不会按照您期望的方式排序....