presorted list that allowes duplicates

  • Thread starter Thread starter bonk
  • Start date Start date
B

bonk

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.
 
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<Thing> items = 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
 
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.
 
A sorted List does nto allow duplicate keys.
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.
 
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.

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<Thing> items = 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 said:
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 -
 
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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top