473,503 Members | 8,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8524
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
20283
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
23516
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
5155
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
1473
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
2571
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
1953
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
1476
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
2226
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
1706
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
7267
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,...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.