2013-07-08 80 views
0

我创建了一个c#我的班级列表与三个字段。该字段还列出设备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

为相同元素的所有列表方面。例如:deviceID [0]指向与deviceMode [0]相同的元素。如果是的话,我会创建一个名为device的基类,它包含了所有的DiviceModes,ID等。这个列表可以很容易地排序。它的优点是,没有人可以“错误地”处理你的数据。 –

+0

虽然时间和模式都是同一类实例的部分,但它们完全不同。所以我不认为排序一个列表会对其他列表产生任何影响。 – Rakesh

+0

检查了这一点:http://stackoverflow.com/questions/7099741/c-sharp-list-sort-by-two-columns,http:// stackoverflow。com/questions/289010/c-sharp-list-sort-by-x-then-y – Arie

回答

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>以便能够通过标识检索数据。

+0

你是在谈论'System.Collections.Generic.SortedList '? – Henrik

+0

将项目添加到“List”然后对其进行排序比将项目添加到“SortedList”更有效。在大多数情况下,对于任何特定的任务,SortedList都不如另一个集合。我几乎从未发现它是任何特定工作的最佳收藏。 – Servy

+0

@亨利克:是的,我错过了第一个通用的论点。谢谢 –

1

您需要以某种方式将列表链接在一起。也许让一个List带有一个Tuple在其中,持有你的项目,然后排序在元组的第一个字段。

可替换地,使用构件TimeDeviceIDDeviceMode一个DeviceLogEntry

你显然想要它的完整性模型。

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(); 
+0

为什么扩大名单?只需直接使用'List'即可。如果你真的,真的不想输出通用名称,那么你可以使用实际的别名而不是从'List'继承。 – Servy

+0

是的,是如此prepupied与试图不改变是忘记基本的布局:))谢谢。 – terrybozzio