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

sorting values in a generic list

Hi I have a generic list populated by a structure and sorts the contents of
the list based on the day and project # that is part of the structure. I
would like to modify it to also sort on the start time as the third sort
parameter? thanks.

public class Comparer : IComparer<mergeoutstruct>
{

public int Compare(mergeoutstruct a, mergeoutstruct b)
{
int result = DateTime.Compare(a.day, b.day);
if (result == 0)
{
result = a.projectnum.CompareTo(b.projectnum);
}
return result;
}

}
public struct mergeoutstruct
{
public Int32 projectnum;
public DateTime day;
public TimeSpan time;
public Double timeout;
public DateTime starttime;
public DateTime endtime;
}

Comparer comp = new Comparer(); //sorts by day and project #.
merglist.Sort(comp.Compare);
--
Paul G
Software engineer.
Oct 9 '07 #1
2 4458
Paul,

You just have to add one more check, like so:

public class Comparer : IComparer<mergeoutstruct>
{
public int Compare(mergeoutstruct a, mergeoutstruct b)
{
int result = DateTime.Compare(a.day, b.day);
if (result == 0)
{
result = a.projectnum.CompareTo(b.projectnum);

// New check.
if (result == 0)
{
// Compare the start time.
result = a.starttime.Compare(b.starttime);
}
}
return result;
}
}

You probably want to compare against the end time as well, in the event
that the start times are the same.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.comwrote in message
news:B6**********************************@microsof t.com...
Hi I have a generic list populated by a structure and sorts the contents
of
the list based on the day and project # that is part of the structure. I
would like to modify it to also sort on the start time as the third sort
parameter? thanks.

public class Comparer : IComparer<mergeoutstruct>
{

public int Compare(mergeoutstruct a, mergeoutstruct b)
{
int result = DateTime.Compare(a.day, b.day);
if (result == 0)
{
result = a.projectnum.CompareTo(b.projectnum);
}
return result;
}

}
public struct mergeoutstruct
{
public Int32 projectnum;
public DateTime day;
public TimeSpan time;
public Double timeout;
public DateTime starttime;
public DateTime endtime;
}

Comparer comp = new Comparer(); //sorts by day and project #.
merglist.Sort(comp.Compare);
--
Paul G
Software engineer.

Oct 9 '07 #2
it works, thanks!
--
Paul G
Software engineer.
"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

You just have to add one more check, like so:

public class Comparer : IComparer<mergeoutstruct>
{
public int Compare(mergeoutstruct a, mergeoutstruct b)
{
int result = DateTime.Compare(a.day, b.day);
if (result == 0)
{
result = a.projectnum.CompareTo(b.projectnum);

// New check.
if (result == 0)
{
// Compare the start time.
result = a.starttime.Compare(b.starttime);
}
}
return result;
}
}

You probably want to compare against the end time as well, in the event
that the start times are the same.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.comwrote in message
news:B6**********************************@microsof t.com...
Hi I have a generic list populated by a structure and sorts the contents
of
the list based on the day and project # that is part of the structure. I
would like to modify it to also sort on the start time as the third sort
parameter? thanks.

public class Comparer : IComparer<mergeoutstruct>
{

public int Compare(mergeoutstruct a, mergeoutstruct b)
{
int result = DateTime.Compare(a.day, b.day);
if (result == 0)
{
result = a.projectnum.CompareTo(b.projectnum);
}
return result;
}

}
public struct mergeoutstruct
{
public Int32 projectnum;
public DateTime day;
public TimeSpan time;
public Double timeout;
public DateTime starttime;
public DateTime endtime;
}

Comparer comp = new Comparer(); //sorts by day and project #.
merglist.Sort(comp.Compare);
--
Paul G
Software engineer.


Oct 9 '07 #3

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

Similar topics

3
by: Leszek Klich | last post by:
Hello All ! I have a task: QT library: List ListBox. I generating n random numbers from range 0 to 100. I have to sort it by hand... It has to look nicely. It's my workhome from my school....
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
3
by: ASP Developer | last post by:
I have a generic list of objects. These objects have a text field that contains a letter for the first character and numbers for the next three. For example, "A001". I need to sort these by...
16
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this:...
10
by: Wing Siu | last post by:
Dear All I would like to develop a generic class sorting function. Currently, when I need sort a collection of user class, for example: Person, Exhibition, Country I need implement a comparer...
4
by: kurt sune | last post by:
I have a an aspx page with a gridview. The gridview is data bound to a generic list of custom classes. The gridview's DataSource is thus not set. Now I want to add sorting to it. So I create...
3
by: Harry Haller | last post by:
Hello, I want to implement a generic list which will be used to display 7 columns in a GridView. One should be able to sort, filter and page each of the 7 columns. Ideally the filter should be...
4
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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
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...

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.