Sort two Lists together as one?

前端 未结 7 2056
执笔经年
执笔经年 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:14

    You could create a custom type to hold a value, or if you only need to use it in the scope of a single method you could use an anonymous type as follows.

    var namesAndDates = MyNames
        .Select((name, i) => new {name, date = MyDates[i]});
    

    From here the two values are no longer loosely tied. Now you can sort them.

    var sortedNamesAndDates = namesAndDates.OrderBy(a => a.date);
    

    Or whatever else you'll need to do with them.

    0 讨论(0)
提交回复
热议问题