C# Sorting list by another list

前端 未结 4 1131
情书的邮戳
情书的邮戳 2020-12-21 23:33

I have now 2 lists:

list names;
list numbers;

and I need to sort my names based on the values in numbers. I\'ve be

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 00:00

    So i assume that the elements in both lists are related through the index.

    names.Select((n, index) => new { Name = n, Index = index })
         .OrderBy(x => numbers.ElementAtOrDefault(x.Index))
         .Select(x => x.Name)
         .ToList();
    

    But i would use another collection type like Dictionary instead if both lists are related insomuch.

提交回复
热议问题