title: Sort a group of Classes by default property
body: <p>TL;DR:</p>
  
  <p><strong>Is there any way to pass a class collection/list to a <em>library</em> sorting algorithm, and get it to return a sorted list (preferably by a named/default class property)?</strong></p>
  
  <p>I've recently been learning some Python, and was impressed by the <code>Sorted()</code> function, which can sort any iterable. For numbers this is straightforward, for classes however, it is possible to assign a comparison method <a href="https://stackoverflow.com/a/4010558/6609896">like this</a>. The method tells comparison operators how to compare 2 instances of the class. Amongst other things it allows you to use builtin sorting algorithms to sort a collection of the class.</p>
  
  <p>In VBA I've been half successful in mimicking this. By setting a class' <a href="http://www.cpearson.com/excel/DefaultMember.aspx" rel="nofollow noreferrer">default member</a> <code>Attribute</code>, you can use comparison operators (<code>&lt;</code>,<code>=</code>,<code>&gt;=</code>, etc.) on classes directly. Take the example class:</p>
  
  <pre><code>VERSION 1.0 CLASS
  BEGIN
    MultiUse = -1  'True
  END
  Attribute VB_Name = "defaultProp"
  Attribute VB_GlobalNameSpace = False
  Attribute VB_Creatable = False
  Attribute VB_PredeclaredId = False
  Attribute VB_Exposed = False
  Private randVal As Single
  
  Public Property Get DefaultValue() As Single
      Attribute Value.VB_UserMemId = 0
      DefaultValue = randVal
  End Property
  
  Private Property Let DefaultValue(ByVal value As Single)
      randVal = value
  End Property
  
  Private Sub Class_Initialize()
      DefaultValue = Rnd()
  End Sub
  </code></pre>
  
  <p>Two instances of this class can be compared:</p>
  
  <pre><code> Dim instance1 As New defaultProp
   Dim instance2 As New defaultProp
   Debug.Print instance1.DefaultValue &gt; instance2.DefaultValue
   Debug.Print instance1 &gt; instance2 'exactly equivalent, as the DefaultValue has the correct Attribute
  </code></pre>
  
  <p>And if I was implementing a VBA sort algorithm that can sort values, there should be no problem sorting classes by default value*. However I would prefer to use a builtin/library sorting algorithm (for the same reasons anyone would; clarity, efficiency, proper error handling etc.)</p>
  
  <p><sub>*One of <a href="http://www.vbforums.com/showthread.php?t=473677" rel="nofollow noreferrer" title="VBA Sorting Algorithms">these algorithms</a> would work for that, although must be modified to switch the entire class round, not the value of it (by adding <code>Set</code>s)</sub></p>
  
  <p>Since VBA comparison operators have no issue, I assumed the same would be true for whatever the library was using. However when I tried with an <a href="https://msdn.microsoft.com/en-us/library/8k6e334t(v=vs.110).aspx" rel="nofollow noreferrer">ArrayList</a>:</p>
  
  <pre><code>Sub testArrayList()
      Dim arr As Object
      Set arr = CreateObject("System.Collections.ArrayList")
  
      ' Initialise the ArrayList, for instance by generating random values
      Dim i As Long
      Dim v As defaultProp
  
      For i = 1 To 5
          Set v = New defaultProp
          arr.Add v 'no problem here
      Next i
      arr.Sort 'raises an error
  End Sub
  </code></pre>
  
  <p>I get an error</p>
  
  <blockquote>
    <p>Failed to compare two elements in the array</p>
  </blockquote>
  
  <p>So what's going on? Is it a flaw in my approach - is the default attribute not making it to the <code>ArrayList</code>? Or maybe the comparison operator in whatever language the library is written in is not as floopy-gloopy as the ones VBA and Python use? Any suggestions on more builtin sorting algorithms to try would be useful too!</p>