Sort Array By Value Of Class Instance

How do I sort the values of an array into an array that is ordered by the values? I’m guessing the best approach is by using dictionaries. Has anybody written a method or could guide me on how I can sort array items by how high the number is of one of its properties. Here is an example:

If I had a class with a property called Layer and I had this array before being processed by code:
Item1 { Layer: 0 }
Item2 { Layer: 3 }
Item3 { Layer: 5 }
Item4 { Layer: 2 }
Item5 { Layer: 2 }

After being processed, the final result is:
Item1 { Layer: 0 }
Item4 { Layer: 2 }
Item5 { Layer: 2 }
Item2 { Layer: 3 }
Item3 { Layer: 5 }

Notice how as you go down each item after it been processed by the desired code, each array item has a higher or identical number to previous. Also, they are in the same order as the original throughout the items with the same Layer number.


Quickly off the top of my head - get the class names with introspection and put them into a parallel array. Then use SortWith to sort the objects.

Where classArray() is an array of your class instances:

[code]dim valueArray() as integer
redim valueArray(classArray.ubound)

for i as integer=0 to classArray.ubound


I use some generic routines for Strings/Integers/Doubles

[code]Sub SortClassArrayByStringValue(extends items() As Object, sortField As String)
Dim keys() As String

Dim typ As Introspection.TypeInfo

Dim prop, props() As Introspection.PropertyInfo

For each oItem As Object in items

typ = Introspection.GetType(oItem)

props = typ.GetProperties

for each prop in props
  if prop.Name = sortField then
    keys.Append prop.Value(oItem)
  end if



End Sub

Thanks for the suggestions. I’m going to try Peter’s method.