There is a list of objects sorted in the order I need. Then I receive a new list in which there are id objects from the first list. The second list is sorted by its. How can I sort it by id_from_firstlist in the order in which they appear in this first list? The first list is sorted by the field which is not in the second list. Now I am doing this in a not quite elegant way. I think there may be some kind of human method without an additional list?

 var firstList = new List<Data1>(); var secondList = new List<Data2>(); var sorted = new List<Data2>(); firstList.Each(zz => { sorted.Add(secondList.First(z => z.id_from_firstlist == zz.Id));}); 
  • why not just sort your second list by the method that you sort the first? - koks_rs
  • The first list is sorted by the field which is not in the second list - Moonvvell
  • 2
    try something like secondList.OrderBy (d => firstList.IndexOf (d.prop_in_first_list)) - koks_rs
  • Use the Array.Sort overload - sorts the second array using the first as keys. - Alexander Petrov
  • var sorted = secondList.OrderBy (list2 => firstList.FindIndex (list1 => list1.id == list2.id)); - Leonid Malyshev

2 answers 2

Sketched a test example :

 public class Test { public static void Main() { List<Item1> myList = Test.getmyList(); List<Item2> externList = Test.getExternList(); Test.print(myList, "Мой список:"); Test.print(externList, "Π’Π½Π΅ΡˆΠ½ΠΈΠΉ список Π΄ΠΎ сортировки:"); externList = externList.OrderBy(x => myList.FindIndex(y => x.Name == y.Name)).ToList(); Test.print(externList, "Π’Π½Π΅ΡˆΠ½ΠΈΠΉ список послС сортировки:"); } private static List<Item1> getmyList() { Item1[] items = new Item1[] { new Item1 {id = 1, Name = "P1"}, new Item1 {id = 2, Name = "P2"}, new Item1 {id = 3, Name = "P3"}, new Item1 {id = 4, Name = "P4"}, new Item1 {id = 5, Name = "P5"} }; return items.ToList(); } private static List<Item2> getExternList() { Item2[] items = new Item2[] { new Item2 { Name = "P5"}, new Item2 { Name = "P4"}, new Item2 { Name = "P3"}, new Item2 { Name = "P2"}, new Item2 { Name = "P1"} }; return items.ToList(); } private static void print<T>(IEnumerable<T> list, string message = "") { Console.WriteLine(message); foreach (var item in list) { Console.WriteLine(item.ToString()); } } } class Item1 { public int id { get; set; } public string Name { get; set; } public override string ToString() { return Name; } } class Item2 { public string Name { get; set; } public override string ToString() { return Name; } } 

    Alternative solution with joyno (you do have joey, right?). A small problem - the query syntax does not support indexes, you have to get them "manually".

    Code:

     class Test { static void Main(string[] args) { var l1 = new[] { new C1() { Id = 3, Name = "Иоганн" }, new C1() { Id = 1, Name = "Ваня" }, new C1() { Id = 2, Name = "Π”ΠΆΠΎΠ½Π½ΠΈ" } }; var l2 = new[] { new C2() { Id = 1, Age = 3 }, new C2() { Id = 3, Age = 331 }, new C2() { Id = 2, Age = 25 } }; var result = from main in l1 join aux in l2.Select((c2, idx) => new { c2.Id, idx }) on main.Id equals aux.Id orderby aux.idx select main; foreach (var c1 in result) Console.WriteLine(c1.Name); } } 

    Classes with data:

     class C1 { public int Id { get; set; } public string Name { get; set; } } class C2 { public int Id { get; set; } public int Age { get; set; } }