473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 8507
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
I'm using an enumerator to iterate through a HashTable that contains all numeric keys. I'd like to iterarate through the HashTable based on the ordered keys. Is there a quick way to do this?...
7
by: MrNobody | last post by:
since ArrayList implements ICollection, is there a quick way to convert an ICollection into ArrayList (besides actually iterating through each element in the ICollection and explicitly adding them...
3
by: andrew.miadowicz | last post by:
It's funny that I've only now run into this question, after a few years of using C#, but I find it intriguiging all the same. All the more so, that the generic version of ICollection in .Net...
3
by: Alexander Widera | last post by:
Hi, I have a problem with this code ... (see below) ... I want to sort an instance of MyList ... by MyData.Shortname ... Shortname is of the type string.... how can I sort the entries? Thank...
3
by: Dave Booker | last post by:
Am I missing something here? It looks like the generic Queue<T> implements Enumerable<T> and ICollection but not ICollection<T>. (I want to use it in an interface that wants an ICollection<T>.) ...
2
by: Allan Ebdrup | last post by:
I have a public sealed Class A that implements the interface I. Now I want to pass an ICollection<Aas a parameter to a method that takes a parameter of the type ICollection<Ibut I get the error: ...
0
by: emma_middlebrook | last post by:
Hi Hopefully the title is quite accurate but here's some more information. I have a load of ICollection<references hanging off a class e.g. ICollection<X>, ICollection<Yetc etc. Each of the...
2
by: per9000 | last post by:
Hi, *background* I want a class containing an int (a list of sets of integer). This should be hidden for the user and he/she should be able to insert his/her favourite data structure so to be a...
4
by: blowdoof | last post by:
I'm facing folowing issue: to be able to print a list of objects in a user-defined way, i've implemented an IComparer (sortclass), which is able to sort any object by its property. I have a list...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.