Sort list based on group count

前端 未结 3 649
忘掉有多难
忘掉有多难 2020-12-21 03:41

I\'d like to sort a List on element counts of IGroupings.

And that\'s it, the list should ideally be the same. I would compromise

3条回答
  •  囚心锁ツ
    2020-12-21 04:11

    Almost no operation in .NET clones an object. Neither deeply nor shallowly. LINQ also does not clone the elements it processes. Therefore, a simple LINQ query will work:

    var oldList = ...;
    var newList = (from x in oldList
                   group x by something into g
                   orderby g.Count()
                   from x in g //flatten the groups
                   select x).ToList();
    

    This code copies references to the original objects. If you believe otherwise, you are probably misinterpreting what you are seeing.

提交回复
热议问题