0

我用三个字段创建了 ac# 我的班级列表。该字段还列出了设备 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 未相对于时间列表排序。怎么能做到这一点。请帮我。对不起,我的英语不好。提前致谢。

4

3 回答 3

5

You should create a simple class that holds the data:

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

[edit]

As Servy pointed out, you can also sort the list itself, instead of enumerating its content.

You can use a 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);

[end of edit]

The key point is to have related data together within a single object. In comparison, synchronizing the data when it's split across several lists may be challenging.

Extra advice:

You should take a look in the many available generic lists. For example, you can also use a Dictionary<int,DeviceLogData> in order to be able to retrieve the data by Id.

于 2013-07-08T13:38:07.540 回答
1

您需要以某种方式将列表链接在一起。也许List用一个 Tuple 在它里面保存你的项目,然后对 tuple 的第一个字段进行排序。

或者,使用DeviceLogEntry成员Time和。DeviceIDDeviceMode

你显然想要完整性,所以要为它建模。

于 2013-07-08T13:38:23.247 回答
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();
于 2013-07-08T14:59:31.207 回答