2

I'm looking for the best way, in term of time performance, to sort multi-dimensional list. I have a multi-dimensional list with millions rows and variable number(1-10) of columns.

List<List<int?>> multiDimensionalList = new List<List<int?>>//();
            {
            new List<int?>  { 0     ,8      ,57     ,5},
            new List<int?>  { 1     ,4      ,2      ,7},
            new List<int?>  { 2     ,0      ,-55    ,9},
            new List<int?>  { 3     ,8      ,57     ,2},
            new List<int?>  { 4     ,2      ,-120   ,4},
            new List<int?>  { 5     ,3      ,57     ,5},
        };
int[] orderParameters = { 2, 3, 1 };

I have to order this list using the order parameters columns in input.

List<List<int?>> sortedExample = new List<List<int?>>
            {
            new List<int?>  {4      ,2      ,-120   ,4},
            new List<int?>  {2      ,0      ,-55    ,9},
            new List<int?>  {1      ,4      ,2      ,7},
            new List<int?>  {3      ,8      ,57     ,2},
            new List<int?>  {5      ,3      ,57     ,5},
            new List<int?>  {0      ,8      ,57     ,5},
        };

Simple implementation using Linq:

List<List<int?>> sortedList = multiDimensionalList
            .OrderBy(x => x[orderParameters[2]])
            .OrderBy(y => y[orderParameters[1]])
            .OrderBy(z => z[orderParameters[0]])
            .ToList();

Do you know a better solution?

This task is requested frequently and it must be single thread.

Performances regard generation of sortedList from multiDimensionalList.

Community
  • 1
  • 1
Domenico
  • 71
  • 1
  • 6
  • `best time performance`; performance regarding reading, or writing, or both?... can you tell us why the performance is critical here? Is the data requested frequently? updated frequently? – Stefan Nov 24 '17 at 09:46
  • Having said that; if it's really time critical I thing this problem can be parallelized and could be executed on your GPU. See https://stackoverflow.com/questions/3969813/which-parallel-sorting-algorithm-has-the-best-average-case-performance for parallel sorting (or wikipedia... or duckduckgo) – Stefan Nov 24 '17 at 09:52
  • 1
    Linq implementation is incorrect. In case you need multi column sort, it should use `ThenBy` after the first `OrderBy`. Speaking about performance and memory consumption, you can try sorting in place using `List` `Sort` method with custom comparer and compare with Linq. – Ivan Stoev Nov 24 '17 at 10:00
  • 1
    @IvanStoev thank you for your suggestion. My Linq implementation works because orderbys are made in opposite order: to sort by 2 then 3 then 1, it sorts by 1, by 3, by 2. – Domenico Nov 24 '17 at 13:03
  • 1
    @Stefan thank you very much. Unfortunately I can't do parallel sorting because this method it's called by other parallel methods. – Domenico Nov 24 '17 at 13:09

0 Answers0