Lexicographically sorted list of lists of strings

前端 未结 4 1571
轻奢々
轻奢々 2021-01-19 05:39

Currently, I am trying to implement a code to generate frequent sequences. In doing so I need to get an in-place sort of a list of lists of strings as follows:



        
4条回答
  •  鱼传尺愫
    2021-01-19 06:21

    If you want to solve with Sort() you may use this approach

    myList.Sort((x, y) => x.Zip(y,(l1,l2) => string.Compare(l1,l2)).FirstOrDefault(c => c != 0));
    

    Otherwise I would concartenate all items into a single string and compare those.

    This is less efficient because the string objects have to be created first.

     myList = myList.OrderBy(string.Concat).ToList();
    

    Sample: https://dotnetfiddle.net/1VmohI

提交回复
热议问题