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

Benoît Minisini gambas at ...1...
Sun May 18 15:46:07 CEST 2014


Le 14/05/2014 21:38, Tobias Boege a écrit :
> 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.

I don't understand that: if the sort field is a parameter of the sort, 
then you can store it inside a static variable just during the sort.

-- 
Benoît Minisini




More information about the User mailing list