Related to, but not exactly a follow up of this question. After fixing some issues discovered in the last review, I added a little more functionality to the
Enumerable class. The problem is, I've never sorted before. I tried (and failed)to implement a few of the standard algorithms before coming up with this. It's not very efficient. It has to check to see if the collection
IsSorted and just keeps looping until it is.
- Is there a way to make this more efficient without using a more advanced algorithm?
- What would be a simple to understand algorithm that is more efficient than this?
I realize that I could simplify some logic if I created some interfaces, but I would like to work with "built in" collections without wrapping everything in a class that implements an interface.
Bonus points to anyone who can tell me what algorithm I ended up using. I just kept testing until it worked.
Public Function Sort(collectionObject As Collection) As Collection Dim item As Variant Dim innerItem As Variant Dim i As Long Dim j As Long Dim index As Long Do Until IsSorted(collectionObject) For i = 1 To collectionObject.Count index = i If IsObject(collectionObject(i)) Then Set item = collectionObject(i) Else item = collectionObject(i) End If For j = i To collectionObject.Count If IsObject(collectionObject(j)) Then Set innerItem = collectionObject(j) Else innerItem = collectionObject(j) End If If item > innerItem Then collectionObject.Add item, After:=j collectionObject.Remove index index = j End If Next j Next i Loop End Function Private Function IsSorted(collectionObject As Collection) As Boolean Dim item As Variant Dim previous As Variant For Each item In collectionObject If item < previous Then IsSorted = False Exit Function End If If IsObject(item) Then Set previous = item Else previous = item End If Next item IsSorted = True End Function