1

I have events and timestamp of the events. I want to keep objects sorted on timestamp. And i want to remove old objects every minutes. Since objects already sorted on timestamp i think RemoveLesserThenKey like function will work best.

I checked sortedlist but it seems it doesnt have such function. Can you give me any advice?

Do i have to write my own sorted list?

EDIT:
1-Events do not come according to time.
2-I want a collection to use sorting structure(Like BST) to remove objects.

Ňɏssa Pøngjǣrdenlarp
  • 37,255
  • 11
  • 50
  • 147
fyo
  • 57
  • 2
  • 11

2 Answers2

1

SortedDictionary<TKey, TValue> is Your friend

Erik Philips
  • 48,663
  • 7
  • 112
  • 142
Jacek Cz
  • 1,799
  • 1
  • 13
  • 22
  • 1
    Since OP seemed like he wanted a specific function to remove lesser keys, I'd just like to add that this could be accomplished simply by iterating through the keys and removing entries after a certain point where items are considered old. He could have also accomplished this with a `SortedList` by removing entries starting at a certain index, but since he is working with data that is not coming in sorted the `SortedDictionary` is the best choice between the two. – Justin Loveless Jun 02 '16 at 18:30
  • Yes, I can remove objects until a condition has met. Since my condition is only about key values, there should be faster way to remove objects. Just removing left branch of the BST kind of way. – fyo Jun 02 '16 at 18:40
  • Does this count as link only? More details to flesh the answer out as to _how_ to use this would be good – D. Ben Knoble Jun 02 '16 at 18:59
0

I think you would best off writing your own sorted linked list for this. Insertions could be done in worst case O(n) time and removing all nodes below a certain threshold would be a simple linear time operation.

As you suggested you could also use a BST. You'd just have to remember to set a new root every time you did a batch removal.

Edit: In thinking about this, a BST would wind up very unbalanced to the right after a few insert/delete rounds. It may not offer much in the way of time savings over a linked list.