[Gambas-user] Sort multidimensional array?
bagonergi at ...626...
Tue Feb 7 09:58:40 CET 2017
See my project DynamicMatrixExample, in Examples of Gambas Software Farm
2017-02-07 0:30 GMT+01:00 Tobias Boege <taboege at ...626...>:
> On Mon, 06 Feb 2017, Riccardo wrote:
> > Hi,
> > Is it possible to use the array.sort() method on a two-dimensional array?
> > > Public arr_Response As New String
> > >
> > Specifically, I'd like to sort the entire array based on the contents of
> the second column something like: arr_Response.sort() ...
> You can call String.Sort() but the result will probably not be what you
> expect. First of all, you have to understand how Gambas handles the type of
> multi-dimensional array you're using. I have given more explanations of
> on this list and elsewhere than I can recount, but I'll do it another time.
> (I'll type it out anew; search the mailing list archive if you want to see
> previous versions of the explanation.)
> The Gambas interpreter can derive array data types from any given data type
> at runtime. Say "X" is any data type that's currently known to the
> (at runtime), then, if you use "X" like a data type in your Gambas code,
> the interpreter will notice that "X" is made of the known data type "X"
> and a pair of brackets and use a code template to create and register a new
> data type, which is "array of X". "X" is a data type created on the fly
> for storing an array of objects of type "X".
> The type "String" is built-in into the interpreter, but if you use
> "String", the mechanics desribed above are invoked. "String" is a
> new data type created from a code templates stored inside the interpreter.
> This template is, if you care to look at the sources, is really just
> the methods of the "Object" class.
> This means "String" is an "Object" which expects to contain elements
> of type "String" -- nothing more. This finally explains why
> String.Sort() does not do anything sensible. Since the Sort() method
> comes directly from the "Object" class, the sorting is done by comparing
> memory addresses of the "String" elements. It does not look at the
> one layer further down.
> So what about your problem? I would advise that you ditch the String.
> Define a new class "MyResponse" or something, so that you can declare your
> array as
> Public arr_Response As New MyResponse
> The advantage is that in your own MyResponse class you can implement the
> _compare() special method  to compare two MyResponse objects by using
> the second field in the response array, as you originally intended.
> After you implement the _compare() method, the Response.Sort() method
> will sort properly. See also this old project  in German, where we
> demonstrate how to sort GridViews (vastly generic, but with some
> using the very same method. There, the column at which to sort can be
> specified at runtime. Note that the version of the project on this site
> uses a *bad practice* of inheriting Variant instead of creating your
> own class. (There may be other shortcomings in that project which I don't
> remember off-hand.)
>  http://gambaswiki.org/wiki/lang/special/compare
>  http://www.gambas-buch.de/dw/doku.php?id=k17:k17.7:k17.7.4:start
> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
More information about the User