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

need help with Compare() function

i have completed a Compare() function to help me sort the contents of
a ListBox. I wanted to give the user of the class the ability to sort
by EITHER ListItem.Text OR ListItem.Value. ALSO sometimes the text
items/values will be integers and sometimes plain text.

to get around this i implemented 2 public properties on my class. 1
property allows the user to select whether they want to sort by
ListItem.Text or By ListItem.Value property. The other property
allows them to select whether the content type is String or Integer.
i am then looking at these properties and using if/else statements.
but i know that my logic here is messy and prone to error (i am a
beginner) so can anyone suggest a better way for me to achieve this?
thanks. heres my function:
public int Compare(object x, object y)

{

if (dm.contentType == ContentType.String)

{

string sx = "";

string sy = "";

if (dm.sortMode == SortMode.Text)

{

sx = ((ListItem) x).Text;

sy = ((ListItem) y).Text;

}

else

{

sx = ((ListItem) x).Value;

sy = ((ListItem) y).Value;

}

return sx.CompareTo(sy) * this.direction;

}

else

{

int sx = 0;

int sy = 0;

if (dm.sortMode == SortMode.Text)

{

sx = int.Parse(((ListItem) x).Text);

sy = int.Parse(((ListItem) y).Text);

}

else

{

sx = int.Parse(((ListItem) x).Value);

sy = int.Parse(((ListItem) y).Value);

}

return sx.CompareTo(sy) * this.direction;

}

}

Nov 18 '05 #1
0 1011

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

Similar topics

3
by: Tommo | last post by:
Hello All, whilst I know how to sort an array I am having problems with my situation, what I would ideally like is something like a 'sort keys' action with Perl hashes. I am reading in data and...
9
by: geronimo_me | last post by:
Hi, I am atempting to compare part of a field with the whole of another field in access. Is this possible? Basically I have 2 tables with the following info: Table1 Field1 = MR.
1
by: Linda | last post by:
Hi, Is there a way to do a "text" (rather than "binary") compareison with the "like" operator, without changing the global "Option Compare" setting? I don't want to risk breaking many, many...
13
by: **Developer** | last post by:
I need to sort the columns of a ListView. Some columns contain dates and others contain integers. What I did once before is in the Compare method I tried date and if that failed I did...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
1
by: anumliaqat | last post by:
hello!!! I am new to this site.can anyone help me in my program.It is of rational number class in c++ having operator overloading.It is not executing and i am not able to find mistakes. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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?
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:
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,...

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.