I have a "manager" class maintaining a list of objects. Each Object has a certain "position", but this is not known to them, only the manager knows about this. The manager must assign each Object a position and maintain its list of Objects sorted according to this "external attribute".
Note that an Object's position can change at any time. Ideally I should be able to immediately get either Element at position X or the position of Element X at any time.
This is C# code. I am wondering what would be a clean or idiomatic way of doing this.
I thought about making an internal class like this:
class SortedElement {
public Element Elem { get; set; }
public int Position { get; set; }
}
And then maintain a list of SortedElements. I don't know, it seems clumsy to me. Two SortedElements could have the same Position for instance. I feel like there's an obvious, clean solution which I'm missing. I could also make the Position a property of the Elements themselves, but it doesn't make sense semantically, meaning there's no reason for them to know about that except making my life easier.
Please make me go facepalm.
EDIT: Following Eric Lippert's advice of listing my requirements, and a good night's sleep, I realized I should opt for a LinkedList<Element>
and use the index as position. Indeed, the most common operations here will be insertion at the beginning and removal anywhere within the container, which are expensive on an array-based container.
Thanks for all replies.