0

I need to store a list of key value pairs. The key value pair will be used by multiple classes, so ideally its something like a list of a structure I define.

Public Structure Person
    Public ID As Integer
    Public Weight As Decimal
End Structure

'all use Person
    class census
    class importpeople
    class determinebestdiet
    etc
  1. I need to sort by value
  2. I need to remove a person (by Person.ID) or add a new key value pair (and re-sort after each add)
  3. I need to sum the total of all values

Seems like often people use dictionaries and then hack around.

michael
  • 2,577
  • 5
  • 39
  • 62

1 Answers1

1

You can use List<Person> to help your work

  1. Using the List.Sort method:
    theList.Sort(Function(x, y) x.age.CompareTo(y.age))
    Using the OrderBy extension method:
    theList = theList.OrderBy(Function(x) x.age).ToList()
    Details can see this post: https://stackoverflow.com/a/11736001/1050927

  2. Just remove from list and call Sort or OrderBy.
    Depends on what you want to do, you can use OrderBy with Skip and Take to get specific nth person after sort.

  3. Just Sum it
    PersonList.Sum(Function(per) per.Weight)

And I believe Dictionary can do the same too, just you can't reuse the Person only.

Prisoner
  • 1,839
  • 2
  • 22
  • 38