寻求支持自定义对象高效排序与过滤的合适数据结构
Great question! Let's break down the best approaches for your scenario—your code makes it clear you're working in VB6, where precompiled, efficient collection handling relies on specific native or practical workarounds.
Sorting: Your Current Approach Is Perfectly Valid
You’re already using the most efficient precompiled solution for sorting custom objects in VB6: System.Collections.ArrayList paired with your clsGridItem class that implements IComparable.
- Your
IComparable_CompareToimplementation correctly handles integer-based sorting byitemIndex - If you ever need to switch sorting rules (e.g., sort by a different property later), you can create a separate class that implements
IComparerand pass it toArrayList.Sort(yourComparer)—this keeps yourclsGridItemclean and lets you swap logic on the fly.
Filtering: Workarounds for the Missing .Filter() Method
You’re right that VB6’s ArrayList doesn’t include a built-in .Filter() method. Here are three efficient, precompiled-friendly ways to handle filtering:
1. Manual Traversal (Flexible for Any Condition)
This is the simplest approach for ad-hoc filtering, and it integrates seamlessly with your existing ArrayList:
Function FilterGridItems(ByVal sourceList As ArrayList, ByVal targetIndex As Long) As ArrayList Set FilterGridItems = New ArrayList Dim item As clsGridItem For Each item In sourceList ' Replace this condition with any filter logic you need (ranges, string matches, etc.) If item.itemIndex = targetIndex Then FilterGridItems.Add item End If Next End Function
No extra dependencies, just straightforward logic you can tweak to fit any filtering requirement.
2. Collection for Fast Key-Based Lookup
If your filtering often involves finding objects by a unique key (like itemIndex), pair your sorted ArrayList with a Collection for near-instant lookups:
' Initialize both collections when populating your data Dim sortedList As ArrayList: Set sortedList = New ArrayList Dim lookupCollection As Collection: Set lookupCollection = New Collection Dim newItem As clsGridItem Set newItem = New clsGridItem newItem.itemIndex = 456 sortedList.Add newItem ' Use the itemIndex as a string key for the Collection lookupCollection.Add newItem, Key:=CStr(newItem.itemIndex) ' When you need to filter/lookup: Dim matchedItem As clsGridItem Set matchedItem = lookupCollection(CStr(targetIndex))
This is far faster than looping through the entire list if you’re doing frequent key-based searches.
3. Third-Party Precompiled Collection Libraries
If you want built-in filtering functionality without writing your own loops, there are commercial precompiled VB6-compatible collection libraries that include methods like .Filter() out of the box. Just ensure the library aligns with your project’s dependency requirements.
Final Recommendation
- Sorting: Stick with
ArrayList+IComparable(orIComparerfor dynamic rules)—it’s the most efficient, native precompiled solution in VB6. - Filtering: Use manual traversal for flexible conditions, or pair your
ArrayListwith aCollectionfor fast key-based lookups.
内容的提问来源于stack exchange,提问作者Greedo

