469,354 Members | 2,170 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,354 developers. It's quick & easy.

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 1968
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by christof hoeke | last post: by
24 posts views Thread by Robin Cole | last post: by
29 posts views Thread by Mathijs | last post: by
Thekid
3 posts views Thread by Thekid | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.