Hi I have a generic list that is populated with a structure. I would like to
be able to sort on values in the structure. I have
public struct mergoutstruct
{
public Int32 projectnumber;
public DateTime day;
public TimeSpan time;
public Double timeout;
}
List<mergeoutstructmergelist;
mergestruct newstruct;
newstruct = new mergeoutstruct();
merglist = new List<mergeoutstruct>();
newstruct.time = 1:0;
newstruct.projectnumber = 11;
newstruct.day= Convert.ToDateTime (9/5/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
newstruct.time = 1:0;
newstruct.projectnumber = 10;
newstruct.day= Convert.ToDateTime (9/5/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
newstruct.time = 2:0;
newstruct.projectnumber = 11;
newstruct.day= Convert.ToDateTime (9/6/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
So I would like to sort the mergelist where the structures or odered by day
and by project number. This would cause the structures in merglist with the
same project number and date to be grouped together,so for the example above
after the sort I would like to see,
merglist[0].projectnumber =10, merglist[1].projectnumber = 11 merglist[2].
projectnumber = 11.
I am not sure if there is a way to do this.
Thanks.
--
Paul G
Software engineer. 9 4463
Paul <Pa**@discussions.microsoft.comwrote:
<snip>
So I would like to sort the mergelist where the structures or odered by day
and by project number. This would cause the structures in merglist with the
same project number and date to be grouped together,so for the example above
after the sort I would like to see,
merglist[0].projectnumber =10, merglist[1].projectnumber = 11 merglist[2].
projectnumber = 11.
I am not sure if there is a way to do this.
I'd suggest using the "Sort" method of List<T>, possibly using an
anonymous method to specify the comparison.
I'd also recommend:
1) Using a class rather than a struct, given that it's mutable
2) Using properties rather than public fields
3) Following .NET naming conventions
4) Using a single DateTime to represent the date and time (as that's
what DateTime is)
5) Providing a constructor which lets you set all the values in one go
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Paul wrote:
Hi I have a generic list that is populated with a structure. I would like to
be able to sort on values in the structure. I have
public struct mergoutstruct
{
public Int32 projectnumber;
public DateTime day;
public TimeSpan time;
public Double timeout;
}
List<mergeoutstructmergelist;
mergestruct newstruct;
newstruct = new mergeoutstruct();
merglist = new List<mergeoutstruct>();
newstruct.time = 1:0;
newstruct.projectnumber = 11;
newstruct.day= Convert.ToDateTime (9/5/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
newstruct.time = 1:0;
newstruct.projectnumber = 10;
newstruct.day= Convert.ToDateTime (9/5/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
newstruct.time = 2:0;
newstruct.projectnumber = 11;
newstruct.day= Convert.ToDateTime (9/6/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
I don't like mutable structs...
public struct MergeOut {
private int _projectNumber;
private DateTime _day;
private TimeSpan _time;
private double _timeOut;
public MergeOut(int projectNumber, DateTime day, TimeSpan, time,
double timeOut) {
_projectNumber = projectNumber;
_day = day;
_time = time;
_timeOut = timeOut;
}
public int ProjectNumber { get { return _projectNumber; } }
public DateTime Day { get { return _day; } }
public TimeSpan Time { get { return _time; } }
public double TimeOut { get { return _timeOut; } }
}
List<MergeOutmergeList = new List<MergeOut>();
mergeList.Add(new MergeOut(11, new DateTime(2007, 9, 5), 1:0, 1.0));
mergeList.Add(new MergeOut(10, new DateTime(2007, 9, 5), 1:0, 1.0));
mergeList.Add(new MergeOut(11, new DateTime(2007, 9, 5), 2:0, 1.0));
So I would like to sort the mergelist where the structures or odered by day
and by project number. This would cause the structures in merglist with the
same project number and date to be grouped together,so for the example above
after the sort I would like to see,
merglist[0].projectnumber =10, merglist[1].projectnumber = 11 merglist[2].
projectnumber = 11.
I am not sure if there is a way to do this.
Thanks.
Of course there is.
Add a comparer to the class:
public class Comparer : IComparer<MergeOut{
public int Compare(MergeOut a, MergeOut b) {
int result = DateTime.Compare(a._day, b._Day);
if (result == 0) {
result = a._projectNumber.CompareTo(b._projectNumber);
}
return result;
}
}
Then you can use that to sort the list:
mergeList.Sort(MergeOut.Comparer);
--
Göran Andersson
_____ http://www.guffa.com
Hi, thanks for the response. I tried using the sort method but could not
feed it the correct arguments. I am kind of new to some of the more advanced
features of C#. I was wondering if you have time could provide a brief
example on some of your suggestions. I think I can create the class ok,
based on the structure I already have.
--
Paul G
Software engineer.
"Jon Skeet [C# MVP]" wrote:
Paul <Pa**@discussions.microsoft.comwrote:
<snip>
So I would like to sort the mergelist where the structures or odered by day
and by project number. This would cause the structures in merglist with the
same project number and date to be grouped together,so for the example above
after the sort I would like to see,
merglist[0].projectnumber =10, merglist[1].projectnumber = 11 merglist[2].
projectnumber = 11.
I am not sure if there is a way to do this.
I'd suggest using the "Sort" method of List<T>, possibly using an
anonymous method to specify the comparison.
I'd also recommend:
1) Using a class rather than a struct, given that it's mutable
2) Using properties rather than public fields
3) Following .NET naming conventions
4) Using a single DateTime to represent the date and time (as that's
what DateTime is)
5) Providing a constructor which lets you set all the values in one go
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Paul <Pa**@discussions.microsoft.comwrote:
Hi, thanks for the response. I tried using the sort method but could not
feed it the correct arguments. I am kind of new to some of the more advanced
features of C#. I was wondering if you have time could provide a brief
example on some of your suggestions. I think I can create the class ok,
based on the structure I already have.
Unfortunately I'm too tired right now (it's half past midnight here)
but if you don't have some full examples tomorrow, I'll post again
then.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
ok thanks will give this a try!
--
Paul G
Software engineer.
"Göran Andersson" wrote:
Paul wrote:
Hi I have a generic list that is populated with a structure. I would like to
be able to sort on values in the structure. I have
public struct mergoutstruct
{
public Int32 projectnumber;
public DateTime day;
public TimeSpan time;
public Double timeout;
}
List<mergeoutstructmergelist;
mergestruct newstruct;
newstruct = new mergeoutstruct();
merglist = new List<mergeoutstruct>();
newstruct.time = 1:0;
newstruct.projectnumber = 11;
newstruct.day= Convert.ToDateTime (9/5/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
newstruct.time = 1:0;
newstruct.projectnumber = 10;
newstruct.day= Convert.ToDateTime (9/5/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
newstruct.time = 2:0;
newstruct.projectnumber = 11;
newstruct.day= Convert.ToDateTime (9/6/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
I don't like mutable structs...
public struct MergeOut {
private int _projectNumber;
private DateTime _day;
private TimeSpan _time;
private double _timeOut;
public MergeOut(int projectNumber, DateTime day, TimeSpan, time,
double timeOut) {
_projectNumber = projectNumber;
_day = day;
_time = time;
_timeOut = timeOut;
}
public int ProjectNumber { get { return _projectNumber; } }
public DateTime Day { get { return _day; } }
public TimeSpan Time { get { return _time; } }
public double TimeOut { get { return _timeOut; } }
}
List<MergeOutmergeList = new List<MergeOut>();
mergeList.Add(new MergeOut(11, new DateTime(2007, 9, 5), 1:0, 1.0));
mergeList.Add(new MergeOut(10, new DateTime(2007, 9, 5), 1:0, 1.0));
mergeList.Add(new MergeOut(11, new DateTime(2007, 9, 5), 2:0, 1.0));
So I would like to sort the mergelist where the structures or odered by day
and by project number. This would cause the structures in merglist with the
same project number and date to be grouped together,so for the example above
after the sort I would like to see,
merglist[0].projectnumber =10, merglist[1].projectnumber = 11 merglist[2].
projectnumber = 11.
I am not sure if there is a way to do this.
Thanks.
Of course there is.
Add a comparer to the class:
public class Comparer : IComparer<MergeOut{
public int Compare(MergeOut a, MergeOut b) {
int result = DateTime.Compare(a._day, b._Day);
if (result == 0) {
result = a._projectNumber.CompareTo(b._projectNumber);
}
return result;
}
}
Then you can use that to sort the list:
mergeList.Sort(MergeOut.Comparer);
--
Göran Andersson
_____ http://www.guffa.com
ok no problem, thanks again.
--
Paul G
Software engineer.
"Jon Skeet [C# MVP]" wrote:
Paul <Pa**@discussions.microsoft.comwrote:
Hi, thanks for the response. I tried using the sort method but could not
feed it the correct arguments. I am kind of new to some of the more advanced
features of C#. I was wondering if you have time could provide a brief
example on some of your suggestions. I think I can create the class ok,
based on the structure I already have.
Unfortunately I'm too tired right now (it's half past midnight here)
but if you don't have some full examples tomorrow, I'll post again
then.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Hi I was trying to set up your example but get the following error on MergeOut,
Class struct or interface must have a return type.
public MergeOut(int projectNumber, DateTime day, TimeSpan time,
double timeOut) {
_projectNumber = projectNumber;
_day = day;
_time = time;
_timeOut = timeOut;
}
--
Paul G
Software engineer.
"Göran Andersson" wrote:
Paul wrote:
Hi I have a generic list that is populated with a structure. I would like to
be able to sort on values in the structure. I have
public struct mergoutstruct
{
public Int32 projectnumber;
public DateTime day;
public TimeSpan time;
public Double timeout;
}
List<mergeoutstructmergelist;
mergestruct newstruct;
newstruct = new mergeoutstruct();
merglist = new List<mergeoutstruct>();
newstruct.time = 1:0;
newstruct.projectnumber = 11;
newstruct.day= Convert.ToDateTime (9/5/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
newstruct.time = 1:0;
newstruct.projectnumber = 10;
newstruct.day= Convert.ToDateTime (9/5/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
newstruct.time = 2:0;
newstruct.projectnumber = 11;
newstruct.day= Convert.ToDateTime (9/6/2007);
newstruct.timeout=1.0;
merglist.Add(newstruct);
I don't like mutable structs...
public struct MergeOut {
private int _projectNumber;
private DateTime _day;
private TimeSpan _time;
private double _timeOut;
public MergeOut(int projectNumber, DateTime day, TimeSpan, time,
double timeOut) {
_projectNumber = projectNumber;
_day = day;
_time = time;
_timeOut = timeOut;
}
public int ProjectNumber { get { return _projectNumber; } }
public DateTime Day { get { return _day; } }
public TimeSpan Time { get { return _time; } }
public double TimeOut { get { return _timeOut; } }
}
List<MergeOutmergeList = new List<MergeOut>();
mergeList.Add(new MergeOut(11, new DateTime(2007, 9, 5), 1:0, 1.0));
mergeList.Add(new MergeOut(10, new DateTime(2007, 9, 5), 1:0, 1.0));
mergeList.Add(new MergeOut(11, new DateTime(2007, 9, 5), 2:0, 1.0));
So I would like to sort the mergelist where the structures or odered by day
and by project number. This would cause the structures in merglist with the
same project number and date to be grouped together,so for the example above
after the sort I would like to see,
merglist[0].projectnumber =10, merglist[1].projectnumber = 11 merglist[2].
projectnumber = 11.
I am not sure if there is a way to do this.
Thanks.
Of course there is.
Add a comparer to the class:
public class Comparer : IComparer<MergeOut{
public int Compare(MergeOut a, MergeOut b) {
int result = DateTime.Compare(a._day, b._Day);
if (result == 0) {
result = a._projectNumber.CompareTo(b._projectNumber);
}
return result;
}
}
Then you can use that to sort the list:
mergeList.Sort(MergeOut.Comparer);
--
Göran Andersson
_____ http://www.guffa.com
Paul wrote:
Hi I was trying to set up your example but get the following error on MergeOut,
Class struct or interface must have a return type.
public MergeOut(int projectNumber, DateTime day, TimeSpan time,
double timeOut) {
_projectNumber = projectNumber;
_day = day;
_time = time;
_timeOut = timeOut;
}
That is the constructor of the class. It should have the same name as
the class.
--
Göran Andersson
_____ http://www.guffa.com
ok so this section I think runs when the class is brought into focus. The
sort seems to be working now, thanks.
--
Paul G
Software engineer.
"Göran Andersson" wrote:
Paul wrote:
Hi I was trying to set up your example but get the following error on MergeOut,
Class struct or interface must have a return type.
public MergeOut(int projectNumber, DateTime day, TimeSpan time,
double timeOut) {
_projectNumber = projectNumber;
_day = day;
_time = time;
_timeOut = timeOut;
}
That is the constructor of the class. It should have the same name as
the class.
--
Göran Andersson
_____ http://www.guffa.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kamilche |
last post by:
I've written a generic sort routine that will sort dictionaries,
lists, or tuples, either by a specified key or by value.
Comments welcome!
import types
def sort(container, key = None,...
|
by: Stewart Rogers |
last post by:
Hi all,
I have been working on an ASP.NET application that is a kind of wizard ( a
list of sequential pages ). We built that application for the CLIENT-A and it
worked fine. After six months...
|
by: Greg Buchholz |
last post by:
/*
I've been experimenting with some generic/polytypic programs, and
I've stumbled on to a problem that I can't quite figure out. In the
program below, I'm trying to define a generic version of...
|
by: snesbit |
last post by:
I have a structure called SearchAreaListItem.
The structure has some properties.
The application implements this as a collection.generic.list(of SearchAreaListItem)
I load the collection up ...
|
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...
|
by: Tigger |
last post by:
I have an object which could be compared to a DataTable/List which I am
trying to genericify.
I've spent about a day so far in refactoring and in the process gone
through some hoops and hit some...
|
by: Peter Olcott |
last post by:
How does not specify the sort criteria for Generic.List ??
The way that this is done in C++ STL is to implement operator<(), how is this
done in C# and DotNet for Generic.List ???
|
by: BombDrop |
last post by:
Can any one help I have a method that will return a List to be bound
as a datasource to a combobox see code for population below. I get the
following error when i try to compile
Error 29 ...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |