473,654 Members | 3,098 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 8550
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.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) ;
}
}
}
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.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
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<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
Aug 23 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
20303
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? Thanks! -Mark IDictionaryEnumerator myEnumerator = myHashTable.GetEnumerator(); while ( myEnumerator.MoveNext() ) { //Please print ordered by Key ... Response.Write("<BR>" + myEnumerator.Key.ToString() + ", " + myEnumerator.Value.ToString())
7
23538
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 to a new ArrayList instance) ??
3
5173
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 Framework 2.0 has this method. It seems to me that the question whether a collection has some element should be answerable by any collection, not just IList or IDictionary. An obvious implementation must exist, since every ICollection inherits...
3
1481
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 you for your help. Alex using System;
3
2576
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>.) Is there a reason for this, or is it just an oversight in .NET 2.0? Is there a computationally easy way to cast/convert a Queue<T> to an ICollection<T>?
2
1960
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: Can't convert from System.Collections.Generic<Ato System.Collections.Generic<I> Shouldn't I be able to pass ICollection<Ato a method that takes ICollection<Iwhen A implements I ?
0
1487
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 collection's types are all derived from a base class Base i.e X : Base, Y : Base etc etc.
2
2241
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 little fancy I thought I'd do a Ctor like this: // this is the "core": private int sets;
4
1709
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 that can be printed (first a pdf is generated trough the object, then passed to the printer), all done by clicking 1 button (print button). The internal ICollection (list of) is perfectly sorted, however, the routine to create and print the...
0
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5622
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1593
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.