[Gambas-user] Feature request: Giving _compare() an optional user data argument
B Bruen
bbruen at ...2308...
Thu May 15 01:40:27 CEST 2014
On Wed, 14 May 2014 21:38:33 +0200
Tobias Boege <taboege at ...626...> wrote:
> 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
>
I would really like to see this too! My attempts at providing a generic sorting mechanism along the lines suggested by Tobi's approach have been less than successful for exactly the same reasons.
--
B Bruen <bbruen at ...2308...>
More information about the User
mailing list