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

presorted list that allowes duplicates

Ist there a collection that holds its data sorted (i.e. inserts items
sorted) AND allows to have duplicate values (by wich is sorted) ?
I would like to store elements in that collection wich should be sorted
by its Property "CreationTime" of type DateTime, where two or more
elements can habe the same CreationTime. Whenever I acess the list I
want to get its items sorted by CreationTime.

Dec 22 '06 #1
7 2090
Hi bonk,
a simple List collection may be good enough for what you need, it has a
sort method which will sort the items any way you like and allow for
duplicates. For example, in the code below the list is sorted in ascending
chronological order for the CreationTime values of the items in the list:

using System;
using System.Collections.Generic;

namespace Application1
{
class Thing
{
private DateTime creationTime;

public Thing(DateTime creationTime)
{
this.creationTime = creationTime;
}

public DateTime CreationTime
{
get
{
return this.creationTime;
}
}
}

class Program
{
static void Main(string[] args)
{
List<Thingitems = new List<Thing>();

Thing t1 = new Thing(new DateTime(2006, 12, 21));
Thing t2 = new Thing(new DateTime(2006, 12, 19));
Thing t3 = new Thing(new DateTime(2006, 12, 24));
Thing t4 = new Thing(new DateTime(2006, 12, 21));

items.Add(t1);
items.Add(t2);
items.Add(t3);
items.Add(t4);

//Sort the items by their creation time
items.Sort(delegate(Thing a, Thing b)
{
return a.CreationTime.CompareTo(b.CreationTime);
});
}
}
}
Mark
--
http://www.markdawson.org
http://themightycoder.spaces.live.com

"bonk" wrote:
Ist there a collection that holds its data sorted (i.e. inserts items
sorted) AND allows to have duplicate values (by wich is sorted) ?
I would like to store elements in that collection wich should be sorted
by its Property "CreationTime" of type DateTime, where two or more
elements can habe the same CreationTime. Whenever I acess the list I
want to get its items sorted by CreationTime.

Dec 22 '06 #2
If your class supports IComparable then you could use a sorted list. It can
be enumrated with foreach but doesnt have an int indexer. Its a key+value
collection so it depends on how you want to use it.

--
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com
"bonk" wrote:
Ist there a collection that holds its data sorted (i.e. inserts items
sorted) AND allows to have duplicate values (by wich is sorted) ?
I would like to store elements in that collection wich should be sorted
by its Property "CreationTime" of type DateTime, where two or more
elements can habe the same CreationTime. Whenever I acess the list I
want to get its items sorted by CreationTime.

Dec 22 '06 #3

A sorted List does nto allow duplicate keys.
On 22 Dez., 10:03, Ciaran O''''Donnell
<CiaranODonn...@discussions.microsoft.comwrote:
If your class supports IComparable then you could use a sorted list. It can
be enumrated with foreach but doesnt have an int indexer. Its a key+value
collection so it depends on how you want to use it.

--
Ciaran O''''Donnellhttp://wannabedeveloper.spaces.live.com

"bonk" wrote:
Ist there a collection that holds its data sorted (i.e. inserts items
sorted) AND allows to have duplicate values (by wich is sorted) ?
I would like to store elements in that collection wich should be sorted
by its Property "CreationTime" of type DateTime, where two or more
elements can habe the same CreationTime. Whenever I acess the list I
want to get its items sorted by CreationTime.- Zitierten Text ausblenden -- Zitierten Text anzeigen -
Dec 22 '06 #4
I have thought about this approach too but it would be rather expensive
to resort the whole ist upon each item instertion. That's why I was was
asking for a list that adds the items sorted rigth away.

On 22 Dez., 09:42, Mark R. Dawson
<MarkRDaw...@discussions.microsoft.comwrote:
Hi bonk,
a simple List collection may be good enough for what you need, it has a
sort method which will sort the items any way you like and allow for
duplicates. For example, in the code below the list is sorted in ascending
chronological order for the CreationTime values of the items in the list:

using System;
using System.Collections.Generic;

namespace Application1
{
class Thing
{
private DateTime creationTime;

public Thing(DateTime creationTime)
{
this.creationTime = creationTime;
}

public DateTime CreationTime
{
get
{
return this.creationTime;
}
}
}

class Program
{
static void Main(string[] args)
{
List<Thingitems = new List<Thing>();

Thing t1 = new Thing(new DateTime(2006, 12, 21));
Thing t2 = new Thing(new DateTime(2006, 12, 19));
Thing t3 = new Thing(new DateTime(2006, 12, 24));
Thing t4 = new Thing(new DateTime(2006, 12, 21));

items.Add(t1);
items.Add(t2);
items.Add(t3);
items.Add(t4);

//Sort the items by their creation time
items.Sort(delegate(Thing a, Thing b)
{
return a.CreationTime.CompareTo(b.CreationTime);
});
}
}

}Mark
--http://www.markdawson.orghttp://themightycoder.spaces.live.com

"bonk" wrote:
Ist there a collection that holds its data sorted (i.e. inserts items
sorted) AND allows to have duplicate values (by wich is sorted) ?
I would like to store elements in that collection wich should be sorted
by its Property "CreationTime" of type DateTime, where two or more
elements can habe the same CreationTime. Whenever I acess the list I
want to get its items sorted by CreationTime.- Zitierten Text ausblenden -- Zitierten Text anzeigen -
Dec 22 '06 #5
Cant you use the objects as the key and value.
--
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com
"bonk" wrote:
>
A sorted List does nto allow duplicate keys.
On 22 Dez., 10:03, Ciaran O''''Donnell
<CiaranODonn...@discussions.microsoft.comwrote:
If your class supports IComparable then you could use a sorted list. It can
be enumrated with foreach but doesnt have an int indexer. Its a key+value
collection so it depends on how you want to use it.

--
Ciaran O''''Donnellhttp://wannabedeveloper.spaces.live.com

"bonk" wrote:
Ist there a collection that holds its data sorted (i.e. inserts items
sorted) AND allows to have duplicate values (by wich is sorted) ?
I would like to store elements in that collection wich should be sorted
by its Property "CreationTime" of type DateTime, where two or more
elements can habe the same CreationTime. Whenever I acess the list I
want to get its items sorted by CreationTime.- Zitierten Text ausblenden -- Zitierten Text anzeigen -

Dec 22 '06 #6
Well, if you encapsulate / inherit a collection, then you could simply
keep a flag somewhere to say "unsorted", and then check this flag in
the this[] and GetEnumerator() members (and anything else that works
by index), sorting if necessary (and clearing the flag); that way, you
could add multiple times, and only face the hit (once) when
subsequently accessing by sequence.

Just a thought,

Marc
Dec 22 '06 #7
Hm, I will try it, I thought though that the SortedList/SortedDictonary
would not allow me to add two objects where the IComparable "says" that
two objects are equal.
On 22 Dez., 10:52, Ciaran O''''Donnell
<CiaranODonn...@discussions.microsoft.comwrote:
Cant you use the objects as the key and value.
--
Ciaran O''''Donnellhttp://wannabedeveloper.spaces.live.com

"bonk" wrote:
A sorted List does nto allow duplicate keys.
On 22 Dez., 10:03, Ciaran O''''Donnell
<CiaranODonn...@discussions.microsoft.comwrote:
If your class supports IComparable then you could use a sorted list. It can
be enumrated with foreach but doesnt have an int indexer. Its a key+value
collection so it depends on how you want to use it.
--
Ciaran O''''Donnellhttp://wannabedeveloper.spaces.live.com
"bonk" wrote:
Ist there a collection that holds its data sorted (i.e. inserts items
sorted) AND allows to have duplicate values (by wich is sorted) ?
I would like to store elements in that collection wich should be sorted
by its Property "CreationTime" of type DateTime, where two or more
elements can habe the same CreationTime. Whenever I acess the list I
want to get its items sorted by CreationTime.- Zitierten Text ausblenden -- Zitierten Text anzeigen -- Zitierten Text ausblenden -- Zitierten Text anzeigen -
Dec 22 '06 #8

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

Similar topics

22
by: christof hoeke | last post by:
hello, this must have come up before, so i am already sorry for asking but a quick googling did not give me any answer. i have a list from which i want a simpler list without the duplicates an...
9
by: Paul | last post by:
Can anyone suggest an efficient way to eliminate duplicate entries in a list? The naive approach below works fine, but is very slow with lists containing tens of thousands of entries: def...
5
by: TimG | last post by:
I have a column that has 75 values, 50 are unique/25 are duplicates. I need to be able to bring back a list of the duplicates, listing all rows even if more than two have the same value. I need to...
3
by: Mark V. | last post by:
Here's what I have and I'm stumped. I have a table that has several thousand names and addresses. I only want to send to one address in a household. So what I would like to do is create a new...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
2
by: Zak McGregor | last post by:
Hi all I have a table, for simplicity's sake containing one field, called unid. for example, select unid, oid from table gives me something like this: unid | oid ---------+---------
29
by: Mathijs | last post by:
Hi, Python beginner here and very much enjoying it. I'm looking for a pythonic way to find how many listmembers are also present in a reference list. Don't count duplicates (eg. if you already...
4
by: moon24 | last post by:
Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7 then the result should be 2 7 1 12 here is what I have:...
3
Thekid
by: Thekid | last post by:
I'm trying to figure out a way to find if there are duplicates in an array. My idea was to take the array as 'a' and make a second array as 'b' and remove the duplicates from 'b' using 'set' and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.