Sort Two Lists with relationship

后端 未结 1 1153
一向
一向 2021-01-29 12:25

As I can sort two lists or two vectors, ie I sort a list (distances) and according to her order as I ordered another list that keeps me indexes. Thanks.

Pd. I\'m working

相关标签:
1条回答
  • 2021-01-29 12:58
    List<decimal> scores = GetScores();
    List<Fruit> fruit = GetFruit();
    
    List<Tuple<decimal, Fruit>> sortedPairs = scores
      .Zip(fruit, (s, f) => Tuple.Create(s, f))
      .OrderBy(x => x.Item1)
      .ToList();
    
    scores = sortedPairs.Select(x => x.Item1).ToList();
    fruit = sortedPairs.Select(x => x.Item2).ToList();
    

    Now all you have to do is implement Zip, OrderBy, Select, ToList and Tuple.

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