Sub SortListByListWithDuplicates(ListToSort As List,Reference As List) As List
Dim M As Map
M.Initialize
Dim ReturnList As List
ReturnList.Initialize
'Add each item in the list to sort to a map, and count the entries
For Each Item As Object In ListToSort
M.Put(Item,M.GetDefault(Item,0) + 1)
Next
'Iterate over the sorted reference list
For Each Item As Object In Reference
Dim ReturnedItemCount As Object = M.Get(Item)
'If there is an item in the map that matches
If ReturnedItemCount <> Null Then
'Add the counted entries to our returnlist
For i = 0 To ReturnedItemCount -1
ReturnList.Add(Item)
Next
End If
Next
Return ReturnList
End Sub