Re: sort ICollection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Gravell

    Re: sort ICollection

    Which version of .NET are you using? With 3.5 9and C# 3) you could use
    ..Cast<IPerson> .OrderBy({some comparison}). Otherwise you will probably
    want to place the data into something like a List<IPerson- after that
    you can use Sort({some comparison}).

    Marc
  • Peter

    #2
    Re: sort ICollection

    Marc Gravell wrote:
    Which version of .NET are you using? With 3.5 9and C# 3) you could
    use .Cast<IPerson>. OrderBy({some comparison}). Otherwise you will
    probably want to place the data into something like a List<IPerson-
    after that you can use Sort({some comparison}).
    Hi - thanks for the info. We are not using 3.5 on this project, so I
    can see that as ICollection really only offers an enumerator we'll need
    to make our own list we can sort.

    Is the syntax with .Cast part of the LINQ stuff?

    /Peter

    Comment

    • Marc Gravell

      #3
      Re: sort ICollection

      Yes; it is an extension method for the non-geenric IEnumerable interface.

      For info, here's an example that should (untested) work with .NET 2.0
      and C# 2;

      Marc

      using System;
      using System.Collecti ons;
      using System.Collecti ons.Generic;

      interface IPerson
      {
      string Name { get; }
      }
      class Person : IPerson
      {
      private readonly string name;
      public string Name { get { return name; } }
      public Person(string name) { this.name = name; }
      }
      class Program
      {
      static void Main(string[] args)
      {
      ICollection people = new IPerson[] { new Person("Fred"), new
      Person("Andy"), new Person("Jo") };

      List<IPersonlis t = new List<IPerson>() ;
      foreach (IPerson person in people)
      {
      list.Add(person );
      }
      list.Sort(deleg ate(IPerson x, IPerson y) { return
      string.Compare( x.Name, y.Name); });
      foreach (IPerson person in list)
      {
      Console.WriteLi ne(person.Name) ;
      }
      }
      }

      Comment

      • Peter

        #4
        Re: sort ICollection

        Marc Gravell wrote:
        Yes; it is an extension method for the non-geenric IEnumerable
        interface.
        >
        For info, here's an example that should (untested) work with .NET 2.0
        and C# 2;
        >
        Marc
        >
        using System;
        using System.Collecti ons;
        using System.Collecti ons.Generic;
        >
        interface IPerson
        {
        string Name { get; }
        }
        class Person : IPerson
        {
        private readonly string name;
        public string Name { get { return name; } }
        public Person(string name) { this.name = name; }
        }
        class Program
        {
        static void Main(string[] args)
        {
        ICollection people = new IPerson[] { new Person("Fred"), new
        Person("Andy"), new Person("Jo") };
        >
        List<IPersonlis t = new List<IPerson>() ;
        foreach (IPerson person in people)
        {
        list.Add(person );
        }
        list.Sort(deleg ate(IPerson x, IPerson y) { return
        string.Compare( x.Name, y.Name); }); foreach (IPerson person
        in list) {
        Console.WriteLi ne(person.Name) ;
        }
        }
        }
        Thanks a lot - that's the sort of thing we'll need to do, although I
        never would have thought about using a delegate like that for the
        comparison.

        /Peter

        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: sort ICollection

          Marc Gravell wrote:
          For info, here's an example that should (untested) work with .NET 2.0
          and C# 2;
          ICollection people = new IPerson[] { new Person("Fred"), new Person("Andy"), new Person("Jo") };
          List<IPersonlis t = new List<IPerson>() ;
          foreach (IPerson person in people)
          {
          list.Add(person );
          }
          Maybe:

          ICollection<IPe rsonpeople = new IPerson[] { new
          Person("Fred"), new Person("Andy"), new Person("Jo") };
          List<IPersonlis t = new List<IPerson>(p eople);

          If it is an ICollection<and not an ICollection.

          Arne

          Comment

          Working...