我创建了一个包含三个字段的c#my class list。该字段还列出了设备ID,设备模式,时间。我按时间排序了我的班级名单。时间列表已成功排序,但设备模式列表未按时间列表排序。我怎样才能实现它。我在下面给出的示例代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PBAttendance.App_Code
{
public class DeviceLogData
{
List<int> deviceID = new List<int> { };
List<int> deviceMode = new List<int> { };
List<DateTime> time = new List<DateTime> { };
public List<int> DeviceID
{
get { return deviceID; }
set { deviceID = value; }
}
public List<int> DeviceMode
{
get { return deviceMode; }
set { deviceMode = value; }
}
public List<DateTime> Time
{
get { return time; }
set { time = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PBAttendance.App_Code
{
public class DeviceLogDataList:List<DeviceLogData>
{
}
}
DeviceLogDataList dvclogDataList = new DeviceLogDataList();
DeviceLogData dvclogData = new DeviceLogData();
dvclogData.DeviceID.Add(1);
dvclogData.DeviceMode.Add(1);
dvclogData.Time.Add(DateTime.ParseExact("10:49", "HH:mm", System.Globalization.CultureInfo.InvariantCulture));
dvclogData.DeviceID.Add(1);
dvclogData.DeviceMode.Add(1);
dvclogData.Time.Add(DateTime.ParseExact("10:49", "HH:mm", System.Globalization.CultureInfo.InvariantCulture));
dvclogData.DeviceID.Add(1);
dvclogData.DeviceMode.Add(2);
dvclogData.Time.Add(DateTime.ParseExact("12:51", "HH:mm", System.Globalization.CultureInfo.InvariantCulture));
dvclogData.DeviceID.Add(1);
dvclogData.DeviceMode.Add(2);
dvclogData.Time.Add(DateTime.ParseExact("09:49", "HH:mm", System.Globalization.CultureInfo.InvariantCulture));
dvclogData.DeviceID.Add(1);
dvclogData.DeviceMode.Add(1);
dvclogData.Time.Add(DateTime.ParseExact("13:49", "HH:mm", System.Globalization.CultureInfo.InvariantCulture));
dvclogDataList.Add(dvclogData);
dvclogDataList[0].Time.Sort();
时间列表完美排序为09:49,10:49,10:49,12:51,13:49但设备模式和设备ID未按时间列表排序。怎么能实现这一点。请帮我。对不起,我的英语不好。提前谢谢。
答案 0 :(得分:5)
您应该创建一个包含数据的简单类:
public class DeviceLogData {
public int DeviceID{get; set;}
public int DeviceMode{get; set;}
public DateTime Time {get; set;}
}
void Foo(){
var theList = new List<DeviceLogData>();
theList.Add(new DeviceLogData{
DeviceID: 42,
DeviceMode: 66,
Time = DateTime.Now
});
...
var ordered = theList.OrderBy(log=>log.Time);
foreach(var log in ordered)
{
DoSomethingWithLog(log);
}
}
<强> [编辑] 强>
正如 Servy 指出的那样,您也可以对列表本身进行排序,而不是枚举其内容。
您可以使用Comparison
:
List<DeviceLogData> theList = new List<DeviceLogData>();
theList.Add(new DeviceLogData{ DeviceID: 42, DeviceMode: 66, Time = DateTime.Now });
theList.Add(new DeviceLogData{ DeviceID: 43, DeviceMode: 67, Time = DateTime.Now });
var comparer = new Comparison<DeviceLogData>(
// Use the Time property to compare the custom object
(log1, log2) => log1.Time.CompareTo(log2.Time)
);
theList.Sort(comparer);
[编辑结束]
关键是在一个对象中将相关数据放在一起。相比之下,当数据分成几个列表时同步数据可能具有挑战性。
额外建议:
您应该查看许多可用的通用列表。例如,您还可以使用Dictionary<int,DeviceLogData>
以便能够按Id检索数据。
答案 1 :(得分:1)
您需要以某种方式将列表链接在一起。也许用一个带有元组的List
来保存你的项目,然后在元组的第一个字段上排序。
或者,与成员DeviceLogEntry
,Time
和DeviceID
建立DeviceMode
。
你显然希望完整性如此模仿。
答案 2 :(得分:0)
也许这可以帮助您完成所需的工作:
public class DeviceLogData
{
private static SortedDictionary<DateTime, string> sorteditems = new SortedDictionary<DateTime, string>();
public int DeviceID { get; set; }
public int DeviceMode { get; set; }
public DateTime Time { get; set; }
public DeviceLogData(int id,int mode,DateTime dt)
{
DeviceID = id;
DeviceMode = mode;
Time = dt;
sorteditems.Add(dt, string.Format("At {0} Device with Id -> {1} and mode -> {2} has logged!", dt, id, mode));
}
public static SortedDictionary<DateTime, string> GetRecords()
{
return sorteditems;
}
}
然后使用它你可以这样做:
List<DeviceLogData> dlr = new List<DeviceLogData>();
dlr.Add(new DeviceLogData(1, 1, DateTime.ParseExact("12:51", "HH:mm", System.Globalization.CultureInfo.InvariantCulture)));
dlr.Add(new DeviceLogData(1, 2, DateTime.ParseExact("10:49", "HH:mm", System.Globalization.CultureInfo.InvariantCulture)));
dlr.Add(new DeviceLogData(1, 1, DateTime.ParseExact("13:49", "HH:mm", System.Globalization.CultureInfo.InvariantCulture)));
dlr.Add(new DeviceLogData(1, 1, DateTime.ParseExact("09:49", "HH:mm", System.Globalization.CultureInfo.InvariantCulture)));
dlr.Add(new DeviceLogData(1, 3, DateTime.ParseExact("09:48", "HH:mm", System.Globalization.CultureInfo.InvariantCulture)));
dlr.Add(new DeviceLogData(1, 1, DateTime.ParseExact("15:31", "HH:mm", System.Globalization.CultureInfo.InvariantCulture)));
foreach (var item in DeviceLogData.GetRecords())
{
Console.WriteLine(item.Value);
}
Console.ReadLine();