locked
Best algorithm for sorting low to high from data provided, RRS feed

  • Question

  • User-1800438376 posted

    hi experts,

    I have data like

    list1<int> contain 2,5,1,4,6,9

    list2<int> contain 5,4,7,8,2,1

    list3<int> contain 8,8,7,4,1,5

    what will the best low to high logic to implements via c# code.

    output low to high

    1,1,1,2,2,4,4,5,5,5,6,7,7,8,8,8,9

    Wednesday, August 6, 2014 10:11 AM

Answers

  • User-802546231 posted
    List<int> list1 = new List<int>(new int[] { 2, 5, 1, 4, 6, 9 });
    List<int> list2 = new List<int>(new int[] { 5, 4, 7, 8, 2, 1 });
    List<int> list3 = new List<int>(new int[] { 8, 8, 7, 4, 1, 5 });
    
    list1.AddRange(list2);
    list1.AddRange(list3);
    list1.Sort();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 10:27 AM
  • User281315223 posted

    In addition to Dr. Acula's suggestion, you can also use the Enumerable.Concat() method to chain multiple collections together and then sort them using an OrderBy() call (as the Sort() method doesn't return anything) :

    // Your lists
    List<int> list1 = new List<int>(new int[] { 2, 5, 1, 4, 6, 9 });
    List<int> list2 = new List<int>(new int[] { 5, 4, 7, 8, 2, 1 });
    List<int> list3 = new List<int>(new int[] { 8, 8, 7, 4, 1, 5 });
    
    // Sorted list
    var sorted = list1.Concat(list2).Concat(list3).OrderBy(x => x);
    

    You can see a working example here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 11:21 AM

All replies

  • User-802546231 posted
    List<int> list1 = new List<int>(new int[] { 2, 5, 1, 4, 6, 9 });
    List<int> list2 = new List<int>(new int[] { 5, 4, 7, 8, 2, 1 });
    List<int> list3 = new List<int>(new int[] { 8, 8, 7, 4, 1, 5 });
    
    list1.AddRange(list2);
    list1.AddRange(list3);
    list1.Sort();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 10:27 AM
  • User281315223 posted

    In addition to Dr. Acula's suggestion, you can also use the Enumerable.Concat() method to chain multiple collections together and then sort them using an OrderBy() call (as the Sort() method doesn't return anything) :

    // Your lists
    List<int> list1 = new List<int>(new int[] { 2, 5, 1, 4, 6, 9 });
    List<int> list2 = new List<int>(new int[] { 5, 4, 7, 8, 2, 1 });
    List<int> list3 = new List<int>(new int[] { 8, 8, 7, 4, 1, 5 });
    
    // Sorted list
    var sorted = list1.Concat(list2).Concat(list3).OrderBy(x => x);
    

    You can see a working example here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2014 11:21 AM