Sort two Lists together as one?

前端 未结 7 2075
执笔经年
执笔经年 2020-12-07 01:30

I have two List which I am accessing by index (e.g. Name[10], Date[10]). They\'re closely tied, so Name[10] is related to Date[1

7条回答
  •  被撕碎了的回忆
    2020-12-07 02:11

    Do yourself a favor and keep the two things together:

    public class NameAndDate {
      public string Name { get; set; }
      public DateTime Date { get; set; }
    }
    

    Then you keep them in one list (List):

    If you want to sort by the name, add a IComparer implementation to NameAndDate, then you can just call Sort() on the list.

    If you want to keep access to the Name and the Date, add accessor methods to Results, like

    public string GetName(int index) {
      return list[i].Name;
    }
    

提交回复
热议问题