473,569 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(c# .NET) sorting a list of items in a specified manner


have a list of card objects, each with a rank (1-13) and a type (hearts,
spades etc.) i would like to sort them in type order (eg. hearts before
spades) how would i go about doing that?

currently i am using an ArrayList as datastructure

i noticed the IComparable interface, can i use it to sort the array in
the specified order.

Thanks in advance

Dec 10 '05 #1
4 5399
Yes, you can. You could implement a "helper" class that implements
IComparable and then the override of Array.Sort that takes an
IComparable object as a parameter.

You could also implement IComparable in your Card class, which is OK if
you don't want any alternate sort order.

Dec 10 '05 #2

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Yes, you can. You could implement a "helper" class that implements
IComparable and then the override of Array.Sort that takes an
IComparable object as a parameter.

That would be IComparer.
Which would be the way to go if you want multiple sort orders.
You could also implement IComparable in your Card class, which is OK if
you don't want any alternate sort order.


Dec 10 '05 #3
I would implement IComparable in the card class. CompareTo() is
supposed to return a value less than 0 if this object comes before the
other, greater than 0 if this object comes after the other, or 0 if
they are equal.

The trick is to express your sort criteria as a comparison function in
such a way that it can compare any two cards: if the suits are
different, compare the suits, otherwise compare the numbers.

BTW, if you're using Visual C# 2005, you can use List<Card> and
IComparable<Car d> instead of ArrayList and IComparable. That'll make
your code simpler, because you won't have to check types in the
comparison function.


Dec 10 '05 #4
Yes... IComparer. Serves me write for responding without first checking
the doc! :-)

Dec 10 '05 #5

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

Similar topics

by: Peter Collinson | last post by:
Hi... Is there any way to style a List Item a different color and size than the <LI> in an Ordered List? I'd like a red super-script number and a dark blue text in a page's footnotes. And this be done using style sheets? -- - Yours truly, Pete Collinson
by: Unknown User | last post by:
How can I separate list items displayed inline by vertical bars | ? If I use a background image that simulate a vertical bar, and I put this bg image behind each list item, the last list item will also have a vertical bar to the right, but it shouldn't, because it is the last one. There should be separators only between list items, and not...
by: deepakp | last post by:
Hi, I'm trying to create a site index page. The page layout will consist of a header, left navigation and content display. The left navigation and content display should look like the image file pasted at the bottom of this page http://www.open365test.net/index_test.html I created a stylesheet for the list items as shown at the top of...
by: David | last post by:
Hello All, I am trying to split a long list of list items ( li ) into 3 groups and append them to a div on the page. Below is the test page that I have created. It's fairly simple but I can't seem to get my head wrapped around it. Anyone here can offer a boost? http://mysite.verizon.net/microweb2/3collist.html David J.
by: RSH | last post by:
Hi, I have a situation where I need to add several "Hidden" properties to list items in a dropdownlist. By default the DropDownList item has two properties with regards to the listitems collection, Text and Value. I need to add a DivisionID, and DepartmentID. I assumed i could simply override the ListItem class and add the additional...
by: John Dalberg | last post by:
using VS 2005. I have an unordered list with list items which are displayed horizontally. Some of the list items have hyper links and some have checkboxes. The problem is that the checkboxes' labels don't align horizontally with the links. I tried to use style="vertical-align: top" and others in the list item and in the checkbox but...
by: pezholio | last post by:
Hi, I've got an inline list with a label and checkbox in each list item, each item comes from a MySQL database and, chances are that, in future, the list will become longer. My problem is that in IE6 and 7 one list item is split over 2 lines (i.e. the label is on one line and the checkbox is on the next line), so it looks kinda ugly, not to...
by: Keith Hughitt | last post by:
For example, If you have a list: <ul> <li>item 1 is short.</li> <li>item 2 is a little bit longer</li> </ul> regardless of the size of the contents of each list item, the element
by: c0mrade | last post by:
I have a problem with getting the list items, below is my hibernate code, after that code there is my method ..and below that is my junit test. How can I make sure that query is executing properly, and how can I check that results actually work .. this query should return couple of pids .. and put them in the list.. now I'm 90 % sure that my list...
by: Frinavale | last post by:
What I'd like to do is take a list and display it's items in a circle. For example (the order of the in the circle doesn't matter..except for Item1, it has to be at the top): Item1 Item2 Item3 Item4 I am hoping to avoid JavaScript for this solution but I don't know if it's possible without JavaScript. Obviously...
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...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
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. ...
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...
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...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 we have to send another system

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.