我不确定如何对包含具有DateTime对象和String对象的对象Array的ArrayList进行排序。我最终试图根据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>
答案 0 :(得分:4)
这是我的建议:
Tuple<T1, T2>
。List<T>
,因为它是一个类型化的列表,因此您可以避免使用强制转换,一般情况下更方便。我列出了以下代码:
//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);
}
);