[Gambas-user] Feature request: Giving _compare() an optional user data argument

Tobias Boege taboege at ...626...
Wed May 14 21:38:33 CEST 2014


Hi Benoit,

when we wanted to sort a 2d array (table) by a specific column, variable at
runtime, we did the following:

 1. Create the class, let's call it "Record", to represent the table
    records which implements a _compare() method and has a SortField
    property which indicates to _compare() which column we want to sort
    by.
 2. Create a class "RecordGroup" which Inherits Object[] and has a
    SortField(Field As Integer, Optional Mode As Integer) method which is
    a multicolumn-aware version of Sort:

    Public Sub SortField(Field As Integer, Optional Mode As Integer)
      Dim iInd As Integer

      For iInd = 0 To Super.Count - 1
        Super[iInd].SortField = Field
      Next
      Super.Sort(Mode)
    End
 3. Then, we must work with RecordGroups which we can sort by, say, column 7
    using myGroup.SortField(7).

You see that we need to set each object's SortField property to the Field
value in order to make the _compare()s work together as we desire. If some
objects "don't get it", we will lose transitivity of our relation and the
sort may fail.

We can't use static properties in the Record class either because we may
want to have multiple groups.

These redundant assignments are what bothers us (also that we need a custom
array type that we called "Group" to hide our clumsiness in communicating
the SortField to each object). [ There are other ways of efficiently grouping
elements but the ones I can think of are pretty obscure - but I'm open for
suggestions! If none: ]

Is it possible to - and what will existing code say if we - change
_compare()'s signature to

  Public Sub _compare(hOther As Object, Optional vPrivate As Variant)

and have a new corresponding optional argument to Object[].Sort() and
MyClass[].Sort()? Sorting by column 7 in the above scenario would boil
down to calling (without groups) Record[].Sort(7).

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list