473,412 Members | 2,174 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,412 software developers and data experts.

How do I use sort for ArrayList here

Hello!

I have a class called Item as follows. I use CompareTo to be able to sort an
ArrayList containg Items on heatNumber. This works fine.
Now to my question. I also want to be able to sort a different ArrayList
containg Items on steelGrade. How is that done because I can only have one
CompareTo for the Item class.

class Item : IComparer
{
int heatNumber;
string steelGrade;

public int HeatNumber
{
set { heatNumber = value; }
get { return heatNumber; }
}
public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;
return this.HeatNumber - item.HeatNumber;
}
else
{
throw new ArgumentException("Object to compare is not a Item
object");
}
}
}

//Tony
Jun 27 '08 #1
5 1263
Simply implement the IComparer interface in a separate class and
provide an instance of it as parameter for the ArrayList.Sort method.

Exmaple:
http://msdn.microsoft.com/de-de/libr...dt(VS.80).aspx
Jun 27 '08 #2
"Tony" <jo*****************@telia.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
I have a class called Item as follows. I use CompareTo to be able to sort
an
ArrayList containg Items on heatNumber. This works fine.
Now to my question. I also want to be able to sort a different ArrayList
containg Items on steelGrade. How is that done because I can only have one
CompareTo for the Item class.

class Item : IComparer
{
int heatNumber;
string steelGrade;

public int HeatNumber
{
set { heatNumber = value; }
get { return heatNumber; }
}
public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;
return this.HeatNumber - item.HeatNumber;
}
else
{
throw new ArgumentException("Object to compare is not a Item
object");
}
}
}
You can write a separate comparer in another class:

public class SteelGradeComparer : IComparer
{
public int Compare (Object x, Object y)
{
Item x1 = x as Item;
Item y1 = y as Item;
if (x1==null || y1==null)
throw new ArgumentException("Wrong type");
return string.Compare(x1.steelGrade, y1.steelGrade);
}
}
....
ArrayList myList = ....;
myList.Sort(new SteelGradeComparer());
Jun 27 '08 #3
Tony wrote:
Hello!

I have a class called Item as follows. I use CompareTo to be able to
sort an ArrayList containg Items on heatNumber. This works fine.
Now to my question. I also want to be able to sort a different
ArrayList containg Items on steelGrade. How is that done because I
can only have one CompareTo for the Item class.

class Item : IComparer
{
int heatNumber;
string steelGrade;

public int HeatNumber
{
set { heatNumber = value; }
get { return heatNumber; }
}
public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;
return this.HeatNumber - item.HeatNumber;
}
else
{
throw new ArgumentException("Object to compare is not a
Item object");
}
}
}

//Tony
Hi Tony,

The answers you got are the correct ones for the question you asked, no
doubt, but.....

If you have the just the hint of the option of moving to C# 3.5 then I
strongly urge you to. There is just so much good stuff around
collections of data, that its just ridiculous, if you do any
aggregation of data at all, be it in XML, Object Lists or databases you
simply owe it to yourself to check it out.

So, in 3.5 you could use a linq extension method and simple lambda to
sort on any field you want without having to write any more comparers
for your class, you could...

alist.OfType<Item>().OrderBy(i =i.SteelGrade);

or better yet, use a generic list and save the casting call.

Regards Tim.
--

Jun 27 '08 #4
On May 27, 12:15 am, "Tim Jarvis" <t...@jarvis.com.auwrote:

<snip>
If you have the just the hint of the option of moving to C# 3.5 then I
strongly urge you to.
I know this sounds like a nitpick, but I think it's worth the
community trying to get this right as early as possible: it's not C#
3.5 - there's no such thing. It's C# 3.0 (or C# 3 for short). If you
want LINQ, you need .NET 3.5 (or download LINQBridge and use C# 3
targeting .NET 2.0).

Jon
Jun 27 '08 #5
Jon Skeet [C# MVP] wrote:
I know this sounds like a nitpick
:-)

Nitpick away, you're perfectly right and its a bad habit I have got
into.

Cheers Tim.

--

Jun 27 '08 #6

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

Similar topics

4
by: Jason | last post by:
Here is an odd issue. I am trying to shed some light on why this is causing a problem. I have an ArrayList. I am binding it to a ListBox control with has its Sort property set to True. If the...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
8
by: GrandpaB | last post by:
I need to sort an Arraylist. The Arraylist is contained in classArt. The Arraylist contains objects that have been defined in a sperate class, objArt. I have attempted to implement the sort using...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
4
by: M Harvey | last post by:
I have an arraylist that contains datetime values. What is the best way to sort this arraylist by date ascending? Thanks, Matt
3
by: jtfaulk | last post by:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional I have a multi-dimensional arraylist, and I would like to sort one level of it but not all. The multi-dimensional arraylist...
4
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
3
by: djp | last post by:
Hi I have to sort arraylist. I tried to do this using this page as a reference: http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm I did it exactly the same way but...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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
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...
0
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...

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.