简体   繁体   English

C#Sort Object [] ArrayList中的数组?

[英]C# Sort Object[] Array within ArrayList?

I am not sure how to go about sorting the ArrayList which contains an object Array which has a DateTime object and a String object. 我不确定如何对包含具有DateTime对象和String对象的对象Array的ArrayList进行排序。 I am ultimately trying to sort the ArrayList based on the first object (DateTime) of the array within the ArrayList. 我最终尝试根据ArrayList中数组的第一个对象(DateTime)对ArrayList进行排序。

I've tried to search around for sorting but articles didn't seem to go into the level of detail or this particular use-case the application is hitting. 我试图搜索排序,但文章似乎没有进入详细程度或应用程序正在打击的特定用例。 I'm not sure if this is the best way to deal with the data either but any help suggestion will certainly be appreciative. 我不确定这是否是处理数据的最佳方式,但任何帮助建议肯定会受到赞赏。

The goal is to read mutliple XML files and combine all the Lap data out of the all the XML files and sort them from oldest to most recent then to create a new XML file with the combined sorted information in it. 目标是读取多个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 Data Example 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>

This are my recommendations: 这是我的建议:

  1. Use a class for the items you want to sort, I suggest a Tuple<T1, T2> . 对要排序的项目使用类,我建议使用Tuple<T1, T2>
  2. Use a List<T> because it is a typed list so you avoid casts and it is more convenient in general. 使用List<T>因为它是一个类型化的列表,所以你可以避免强制转换,一般来说它更方便。
  3. We are going to use Linq to sort the array just for easy writting. 我们将使用Linq对数组进行排序,以便于编写。

I list the code below: 我列出下面的代码:

//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();

If you can't use tuples, declare you own class for the item. 如果您不能使用元组,请声明您拥有该项目的类。 For example 例如

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

With this class you can do a easy migration of the code as follows: 使用此类,您可以按如下方式轻松迁移代码:

//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();

If you can't use Linq, here is a non Linq alternative: 如果您不能使用Linq,这里是非Linq替代方案:

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

Or for the case of using the Lap class: 或者对于使用Lap类的情况:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM