简体   繁体   中英

Best data structure that supports sorted

I'm currently trying to figure out which data structure might be the best one to use. So here is what I am trying to do:

I have an object and a value associated with this object. I want to be able to know which entry in the structure has the smallest value.

So for example if I have the following objects:

 ZebraObject, 10
 CowObject, 1
 DogObject, 2

I want to be able to know which object has the smallest value (which in this case, is CowObject). I'll also have to access the data inside the CowObject (call some functions, do some calculation etc) and at the end, I'll be doing something like 'value += value'. So after I've accessed the CowObject, the data will look like

 ZebraObject, 10
 CowObject, 2 // (1 + 1)
 DogObject, 2

Can anyone help me figure out what the best data structure for this situation?

Edit: I'm assuming that every elements (at least for the object), are unique. The float value associated with the object can be duplicates.

Sorted Set is helpful to fulfill your requirement. But sorted set not allow duplicates. For sorting depending on specific field of object Implement IComparer.

you may get more help from SortedSet and equality

ObservableCollection is the best way if you will bind to the list. it already implements the INotifyPropertyChanged interface. Maybe make your Viewmodel inherit from the observablecollection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM