#1
  1. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,517
    Rep Power
    1428

    Sorting List<T> inside variant type method


    Good evening together!

    Currently I'm playing around a little bit with C# and I can't figure out a little gotcha.

    What I'm trying to do is to write a method to group and aggregate a given list, using a lamba function, one for sorting and another to to the aggregation.

    This is a sample code excerpt to explain:

    Code:
    using System;
    using System.Collections.Generic;
    					
    public class GroupHelper
    {
        public static IList<T> groupList<T>(List<T> lst, Comparison<T> cmp, Action<T,T> aggregate)
        {
            List<T> groupedList    = new List<T>();
            T       buffer         = default(T);
            
            lst.sort(cmp);
    
            foreach ( T currItem in lst )
            {
                if( buffer == null )
                {
                    buffer=currItem; 
                    continue;    
                }
        
                if( cmp(buffer, currItem) == 0 )
                {
                    aggregate(buffer, currItem);
                    continue;
                }
    
                groupedList.add(buffer);
                buffer=currItem;
            }
            groupedList.add(buffer);
            return groupedList;
        }
    }
    Unfortunately the whole thing doesn't want to compile. Problems are reported for these three lines:

    Code:
    lst.sort(cmp);
    and the two
    Code:
    groupedList.add(buffer);
    The messages say, 'List<T>' would not contain a definition for 'sort' and 'add', but according to MSDN the indeed do.

    My google skills are leaving me today and I can't say, I have much C# experience. Can anybody please point me into the right direction?

    Relevant links would be appreciated, as well.

    Thanks for any ideas and best regards,
    Jens
    Last edited by JClasen; January 4th, 2019 at 03:45 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,517
    Rep Power
    1428
    Problem solved. I should have taken into account, that C# is case sensitive and Sort() and Add() have to be spelled with capital first letters.

    Regards, J.

    Comments on this post

    • bubba02 agrees

IMN logo majestic logo threadwatch logo seochat tools logo