Skip to main content
 首页 » 编程设计

C# 对 ArrayList 中的对象 [] 数组进行排序

2024年02月13日14luoye11

我不确定如何对 ArrayList 进行排序,其中包含一个对象 Array,该对象 Array 具有一个 DateTime 对象和一个 String 对象。我最终尝试根据 ArrayList 中数组的第一个对象 (DateTime) 对 ArrayList 进行排序。

我尝试四处搜索排序,但文章似乎没有深入到详细程度或应用程序遇到的这个特定用例。我不确定这是否是处理数据的最佳方式,但任何帮助建议都将不胜感激。

目标是读取多个 XML 文件并将所有 XML 文件中的所有 Lap 数据组合起来,并将它们从最旧到最新排序,然后创建一个新的 XML 文件,其中包含组合后的排序信息。

ArrayList LapsArrayList = new ArrayList(); 
 
ListBox.SelectedObjectCollection SelectedItems = lstSelectedFiles.SelectedItems; 
 
foreach (string Selected in SelectedItems) 
{ 
    Object[] LapArray = new Object[2]; 
 
    XmlDocument xDoc = new XmlDocument(); 
    xDoc.Load(Path + @"\" + Selected); 
 
    XmlNodeList Laps = xDoc.GetElementsByTagName("Lap"); 
 
    foreach (XmlElement Lap in Laps) 
    { 
        LapArray[0] = DateTime.Parse(Lap.Attributes[0].Value); 
        LapArray[1] = Lap.InnerXml.ToString(); 
        LapsArrayList.Add(LapArray); 
    } 
} 

XML 数据示例

<Lap StartTime="2013-06-17T12:27:21Z"><TotalTimeSeconds>12705.21</TotalTimeSeconds><DistanceMeters>91735.562500</DistanceMeters><MaximumSpeed>10.839000</MaximumSpeed><Calories>3135</Calories><AverageHeartRateBpm><Value>151</Value>.....</Lap> 

请您参考如下方法:

这是我的建议:

  1. 为要排序的项目使用类,我建议使用 Tuple<T1, T2> .
  2. 使用 List<T> 因为它是一个类型列表,所以您可以避免强制转换,而且通常更方便。
  3. 我们将使用 Linq 对数组进行排序,以便于编写。

我列出下面的代码:

//I dunno what does this has to do, but I'll leave it here 
ListBox.SelectedObjectCollection SelectedItems = lstSelectedFiles.SelectedItems; 
 
//We are going to use a List<T> instead of ArrayList 
//also we are going to use Tuple<DateTime, String> for the items 
var LapsList = new List<Tuple<DateTime, String>>(); 
 
foreach (string Selected in SelectedItems) 
{ 
    XmlDocument xDoc = new XmlDocument(); 
    xDoc.Load(Path + @"\" + Selected); 
    XmlNodeList Laps = xDoc.GetElementsByTagName("Lap"); 
    foreach (XmlElement Lap in Laps) 
    { 
        var dateTime = DateTime.Parse(Lap.Attributes[0].Value); 
        var str = Lap.InnerXml.ToString(); 
        //Here we create the tuple and add it 
        LapsList.Add(new Tuple<DateTime, String>(dateTime, str)); 
    } 
} 
 
//We are sorting with Linq 
LapsList = LapsList.OrderBy(lap => lap.Item1).ToList(); 

如果您不能使用元组,请为该项目声明您自己的类。例如

class Lap 
{ 
    private DateTime _dateTime; 
    private String _string; 
 
    public Lap (DateTime dateTimeValue, String stringValue) 
    { 
        _dateTime = dateTimeValue; 
        _string = stringValue; 
    } 
 
    public DateTime DateTimeValue 
    { 
        get 
        { 
            return _dateTime; 
        } 
        set 
        { 
            _dateTime = value; 
        } 
    } 
 
    public String StringValue 
    { 
        get 
        { 
            return _string; 
        } 
        set 
        { 
            _string = value; 
        } 
    } 
} 

使用此类,您可以按如下方式轻松迁移代码:

//I dunno what does this has to do, but I'll leave it here 
ListBox.SelectedObjectCollection SelectedItems = lstSelectedFiles.SelectedItems; 
 
//We are going to use a List<T> instead of ArrayList 
//also we are going to use the Laps class for the items 
var LapsList = new List<Lap>(); 
 
foreach (string Selected in SelectedItems) 
{ 
    XmlDocument xDoc = new XmlDocument(); 
    xDoc.Load(Path + @"\" + Selected); 
    XmlNodeList Laps = xDoc.GetElementsByTagName("Lap"); 
    foreach (XmlElement Lap in Laps) 
    { 
        var dateTime = DateTime.Parse(Lap.Attributes[0].Value); 
        var str = Lap.InnerXml.ToString(); 
        //Here we create the Lap object and add it 
        LapsList.Add(new Lap(dateTime, str)); 
    } 
} 
 
//We are sorting with Linq 
LapsList = LapsList.OrderBy(lap => lap.DateTimeValue).ToList(); 

如果您不能使用 Linq,这里有一个非 Linq 的替代方案:

LapsList.Sort 
( 
    delegate(Tuple<DateTime, String> p1, Tuple<DateTime, String> p2) 
    { 
        return p1.Item1.CompareTo(p2.Item1); 
    } 
); 

或者对于使用 Lap 类的情况:

LapsList.Sort 
( 
    delegate(Lap p1, Lap p2) 
    { 
        return p1.DateTimeValue.CompareTo(p2.DateTimeValue); 
    } 
);