473,395 Members | 1,458 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,395 software developers and data experts.

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

Hi

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

Anders
Dec 10 '05 #1
4 5393
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*******@canada.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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.


Yup

Bill
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<Card> instead of ArrayList and IComparable. That'll make
your code simpler, because you won't have to check types in the
comparison function.

Jesse

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

5
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...
2
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...
2
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...
7
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...
1
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...
0
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...
5
pezholio
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...
4
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
0
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,...
16
Frinavale
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 ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...
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...

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.