Answered by:
How to Sort 2 different string list into another List array

Question
-
User321489315 posted
Hi All,
I am getting bit complexity while sorting 2 different list of string arrays into another list of string array. Please help me.
I have below 2 different lists
List<string> student1 = new List<string> { "RoleNo1", "RoleNo5", "RoleNo3" };
List<string> student2 = new List<string> { "RoleNo2", "RoleNo4" };i want output of above 2 strings into another list with sorted order like below
List<string> finalStudentOrder = new List<string> { "RoleNo1", "RoleNo2", "RoleNo3", "RoleNo4", "RoleNo5" };
Please help me to achieve this.
Thanks in Advance,
Chandu
Tuesday, July 10, 2018 2:12 PM
Answers
-
User-330142929 posted
Hi Chandu_king,
According to your description, I have made a demo, wish it is useful to you.
List<string> student1 = new List<string> { "RoleNo1", "RoleNo5", "RoleNo3" }; List<string> student2 = new List<string> { "RoleNo2", "RoleNo4" }; List<string> finalStudentOrder = student1.Concat(student2).OrderBy(x => x.Substring(x.Length - 1, 1)).ToList(); foreach (var item in finalStudentOrder) { Console.WriteLine(item); }
Because the List<T> implements The list<T> interface, we could use the Concat method to achieve the purpose of connecting two objects. Then we could use orderby interface to achieve sorting.
Feel free to let me know if you have any question.
Best Regards
Abraham
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 10, 2018 3:54 PM
All replies
-
User753101303 posted
Hi,
I would do it this way :
using System; using System.Collections.Generic; namespace ConsoleApp1 { class Program { static void Main(string[] args) { List<string> student1 = new List<string> { "RoleNo1", "RoleNo5", "RoleNo3" }; List<string> student2 = new List<string> { "RoleNo2", "RoleNo4" }; List<string> finalStudentOrder = new List<string>(student1); // create a new list from list 1 finalStudentOrder.AddRange(student2); // append list 2 finalStudentOrder.Sort(); // sort foreach(var item in finalStudentOrder) { Console.WriteLine(item); } } } }
Or is this what you find a bit complex? Another option would be to use Linq. Else be explicit about what you tried and which kind of complexity you ran into.
Tuesday, July 10, 2018 2:32 PM -
User-330142929 posted
Hi Chandu_king,
According to your description, I have made a demo, wish it is useful to you.
List<string> student1 = new List<string> { "RoleNo1", "RoleNo5", "RoleNo3" }; List<string> student2 = new List<string> { "RoleNo2", "RoleNo4" }; List<string> finalStudentOrder = student1.Concat(student2).OrderBy(x => x.Substring(x.Length - 1, 1)).ToList(); foreach (var item in finalStudentOrder) { Console.WriteLine(item); }
Because the List<T> implements The list<T> interface, we could use the Concat method to achieve the purpose of connecting two objects. Then we could use orderby interface to achieve sorting.
Feel free to let me know if you have any question.
Best Regards
Abraham
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 10, 2018 3:54 PM