1

I need a bit help here I have an array I need to sort in different threads. When the sorting is over, I get a few sublists of sorted elements, f.e.

-124.5 -9.03 0 13.2

-99.3 12.6 189.034 200.0

notice, there can be more sublists If there is any way I can merge these sublists into one saving the sorting order(From the smallest element to the biggest one) in resulted list?


SOLVED

used a piece of code from previous project

    public static T[] ConcatArrs<T>(T[] left, T[] right) where T : System.IComparable<T>
    {
        T[] result = new T[left.Length + right.Length];
        int lc = 0, rc = 0, i = 0;

        for(i = 0; i < left.Length + right.Length; i++)
        {
            if (lc == left.Length)
            {
                result[i] = right[rc];
                rc++;
                continue;
            }

            if (rc == right.Length)
            {
                result[i] = left[lc];
                lc++;
                continue;
            }

            if (left[lc].CompareTo(right[rc]) <=0 )
                result[i] = left[lc++];
            else
                result[i] = right[rc++];
        }

        return result;
    }

1 Answers1

0

You can use the AddRange() method from List<T> to achieve this, as it preservers the order.

var firstList = new List<double> { -124.5, -9.03, 0, 13.2, };
var secondList = new List<double> {-99.3, 12.6, 189.034, 200.0};

firstList.AddRange(secondList);
Jakub Jankowski
  • 731
  • 2
  • 9
  • 20
  • Well, It just concats two lists, I get result as 1 list + 2 list when I need to get new list sorted from the smallest to the biggest – Bogdan Brizhaty Oct 04 '16 at 11:51