[Gambas-user] gb3: sorting an array-like structure
Bruce Bruen
bbruen at ...2308...
Tue May 24 06:01:45 CEST 2011
On 24/05/11 11:52, Kevin Fishburne wrote:
> On 05/23/2011 10:01 PM, Kevin Fishburne wrote:
>
>> On 05/09/2011 09:59 PM, Benoît Minisini wrote:
>>
>>>> Think of it like a spreadsheet instead of an array. I need each "row"
>>>> sorted by the value in a "column". The two column values are
>>>> ObjectNumber and ObjectY and the row represents the array index.
>>>>
>>>> There's got to be something like this in GAMBAS, but if not I can start
>>>> Google searching sort algorithms and make a procedure.
>>>>
>>> - Put the objects you have to draw on the screen in a temporary array.
>>> - Define the (X,Y) final coordinates in two object variables.
>>> - Implement the "_sort" special method in the object class.
>>>
>>> That way, you just have to call "Sort" to sort only the objects you want the
>>> way you want.
>>>
>> The documentation didn't do much to alleviate my confusion, so if anyone
>> understands this could you please post the simplest example possible? A
>> link to the documentation on the _sort special method might be enough (I
>> couldn't find it). As a refresher I'm basically trying to sort an array
>> by one of its elements, much like sorting a spreadsheet
>>
> Looks like Matti had the same question back in 2009:
>
> http://old.nabble.com/sort-multidimensional-arrays-td26051359.html#a26059190
>
> The proposed solution sounds similar to the response given to me, though
> my feeble brain isn't able to convert it into code. Interesting that the
> post mentions a variant can be used as well as an object.
>
> Matti, if you're out there, do you have an example of how you sorted the
> example array in your post using these (or other) techniques?
>
>
Kevin,
Here is the code I use to sort a tableview. It is constrained by
a) only coping with string or numeric data
b) only sorts ascending.
but it may get you started
PUBLIC SUB SELF_ColumnClick(Col AS Integer)
'==========================================
' Sort the underlying gridview by the selected column. NB this will only
' sort the display data - any onscreen formatting that relies
' on the underlying data will refer to the "wrong row"!
' These are the two native arrays that we will sort using their own sort
method
DIM sarray AS NEW String[ME.Rows.Count]
DIM narray AS NEW Float[ME.Rows.Count]
' We need a few indices to operate on the orginal array and the sort array
DIM idx, idy AS Integer
DIM ida, idv AS Integer
' This is a flag to tell us which sort array to use
DIM numeric AS Boolean = TRUE
' First figure out if the column only contains numbers. If not then we
will
' just use a string sort
FOR idx = 0 TO ME.Rows.Count - 1
IF NOT IsNumber(Val(ME[idx, Col].Text)) THEN numeric = FALSE
NEXT
' Now unload each item from the original array into the relevant sort array
FOR idx = 0 TO ME.Rows.Count - 1
IF numeric
narray[idx] = Val(ME[idx, Col].Text)
ELSE
sarray[idx] = ME[idx, Col].Text
ENDIF
NEXT
' and sort it!
IF numeric
narray.Sort(gb.Ascent)
ELSE
sarray.Sort(gb.Ascent + gb.Text)
ENDIF
' Now the tricky bit, use the sorted sort array to reorder the original
array
FOR ida = 0 TO sarray.Max ' upper limit is the
size of any of the arrays
' idv is the row offset for the original array
' in each loop, we only have to look "downwards" from where we are
in the
' sort array ( i.e. from ida onwards) and no need to do the last row!
FOR idv = ida TO ME.Rows.Count - 1
IF IIf(numeric, narray[ida], sarray[ida]) = ME[idv, col].Text
' have we found the row?
IF ida <> idv
' is it in the wrong place?
FOR idx = 0 TO ME.Columns.Count - 1
' then swap it
SWAP ME[idv, idx].Text, ME[ida, idx].Text
NEXT
ENDIF
BREAK
' and stop looking for it
ENDIF
NEXT ' inc idv - haven't found it so keep looking
NEXT ' inc ida - loop back for the next sorted row
END
More information about the User
mailing list