2013-07-08 79 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