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

本文关键字:数组 对象 排序 中的 ArrayList | 更新日期: 2023-09-27 18:19:42

我不知道如何对包含对象Array的ArrayList进行排序,该对象具有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>

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

这是我的建议:

  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);
    }
);