471,349 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

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
Aug 21 '08 #1
4 8240
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
Aug 21 '08 #2
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.Collections;
using System.Collections.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<IPersonlist = new List<IPerson>();
foreach (IPerson person in people)
{
list.Add(person);
}
list.Sort(delegate(IPerson x, IPerson y) { return
string.Compare(x.Name, y.Name); });
foreach (IPerson person in list)
{
Console.WriteLine(person.Name);
}
}
}
Aug 21 '08 #3
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.Collections;
using System.Collections.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<IPersonlist = new List<IPerson>();
foreach (IPerson person in people)
{
list.Add(person);
}
list.Sort(delegate(IPerson x, IPerson y) { return
string.Compare(x.Name, y.Name); }); foreach (IPerson person
in list) {
Console.WriteLine(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
Aug 21 '08 #4
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<IPersonlist = new List<IPerson>();
foreach (IPerson person in people)
{
list.Add(person);
}
Maybe:

ICollection<IPersonpeople = new IPerson[] { new
Person("Fred"), new Person("Andy"), new Person("Jo") };
List<IPersonlist = new List<IPerson>(people);

If it is an ICollection<and not an ICollection.

Arne
Aug 23 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark | last post: by
7 posts views Thread by MrNobody | last post: by
3 posts views Thread by andrew.miadowicz | last post: by
3 posts views Thread by Alexander Widera | last post: by
2 posts views Thread by Allan Ebdrup | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.