Sort multidimensional array?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Sort multidimensional array?

Riccardo
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[][1].sort() ...




 cheers.

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Matti
Hi Riccardo,

this is a question I had long years ago. At this time it was not possible.
As I remember, I used a dirty workaround, reducing the two-dimensionsional array by inserting a "/" to a one-dimensional array, sorting it and removing the "/" later.

Depends on what array you have to sort.

Example for the workaround is attatched. Look at the FMain 'mnuSortA' and 'mnuSortT' Subs.

Hope this helps, but I'm really not up to date.

Matti

Am 06.02.2017 um 22:23 schrieb Riccardo:

> 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[][1].sort() ...
>
>
>
>
>   cheers.
>
> ------------------------------------------------------------------------------
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user

Lighttable-0.2.23.tar.gz (63K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Tobias Boege-2
In reply to this post by Riccardo
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[][1].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 this
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 interpreter
(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 reusing
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 Strings
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 [1] 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 [2] in German, where we
demonstrate how to sort GridViews (vastly generic, but with some restrictions)
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.)

Regards,
Tobi

[1] http://gambaswiki.org/wiki/lang/special/compare
[2] 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Gianluigi
Hi Richard,

See my project DynamicMatrixExample, in Examples of Gambas Software Farm

Regards

Gianluigi

2017-02-07 0:30 GMT+01:00 Tobias Boege <[hidden email]>:

> 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[][1].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
> this
> 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
> interpreter
> (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
> reusing
> 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
> Strings
> 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 [1] 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 [2] in German, where we
> demonstrate how to sort GridViews (vastly generic, but with some
> restrictions)
> 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.)
>
> Regards,
> Tobi
>
> [1] http://gambaswiki.org/wiki/lang/special/compare
> [2] 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Rolf-Werner Eilert-2
In reply to this post by Matti
Hi Matti,

I don't find your method so dirty - I have used it a lot of times
myself. And I use it in table-like arrays with more than 2 columns, all
as strings.

However, I use a character which does not appear in normal strings, like
TAB (chr$(9)) or chr$(6).

To pack and unpack a single line, I use Split and Join which work so
incredibly fast they can be used within milliseconds over thousands of
strings.

As my rule of thumb, when I have a fixed number of columns, I make a
number of named one-dimensional arrays, one for each column. If the
number of columns is not known, I make a single one-dimensional array of
strings and split the columns as described above.

Regards
Rolf

Am 06.02.2017 23:26, schrieb Matti:

> Hi Riccardo,
>
> this is a question I had long years ago. At this time it was not possible.
> As I remember, I used a dirty workaround, reducing the
> two-dimensionsional array by inserting a "/" to a one-dimensional array,
> sorting it and removing the "/" later.
>
> Depends on what array you have to sort.
>
> Example for the workaround is attatched. Look at the FMain 'mnuSortA'
> and 'mnuSortT' Subs.
>
> Hope this helps, but I'm really not up to date.
>
> Matti
>
> Am 06.02.2017 um 22:23 schrieb Riccardo:
>> 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[][1].sort() ...
>>
>>
>>
>>
>>   cheers.
>>
>> ------------------------------------------------------------------------------
>>
>> 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
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
>
>
> ------------------------------------------------------------------------------
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Riccardo
This post was updated on .
In reply to this post by Tobias Boege-2
Thank you very much, Tobi, for the insights, and the link to the "GridView – Sortierung der Daten"

Based on that I tried to implement the following, to test the concept. But the values for the rows (B) are the same in all cases (always 0 ...) as if the variable "i" wasn't incrementing in the loop (but it is).

What am I missing...where have I gone wrong?

  Dim A As New Variant[]
  Dim B As New Variant[]
  Dim i, j As Integer
 
  For i = 0 To 9
    For j = 0 To 19
      B.Add((i))
    Next
    A.Add(B)
  Next
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Riccardo
Note the typo, it should read "B.Add(i)" ... the problem persists either way


7. Feb 2017 20:28 by [hidden email]:


> Thank you very much, Tobi, for the insights, and the link to the "GridView – Sortierung der Daten"
>
> Based on that I tried to implement the following, to test the concept. But the values for the rows (B) are the same in all cases (always 0 ...) as if the variable "i" wasn't incrementing in the loop (but it is).
>
> What am I missing...where have I gone wrong?
>
>   Dim A As New Variant[]
>   Dim B As New Variant[]
>   Dim i, j As Integer
>  
>   For i = 0 To 9
>     For j = 0 To 19
>       B.Add((ij))
>     Next
>     A.Add(B)
>   Next >
>
>
> > > If you reply to this email, your message will be added to the discussion below:> > http://gambas.8142.n7.nabble.com/Sort-multidimensional-array-tp58403p58416.html> > > To unsubscribe from Sort multidimensional array?, > click here> .
> > NAML>



--
View this message in context: http://gambas.8142.n7.nabble.com/Sort-multidimensional-array-tp58403p58417.html
Sent from the gambas-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Riccardo
In reply to this post by Matti
Hi Matti,

Thank you for your response. I had been considering a similar approach, as it allows me to leave out the columns I wouldn't need. So it's great to see your robust implementation of it.

cheers.
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Riccardo
In reply to this post by Gianluigi
Thank you Gianluigi - your DynamicMatrixExmaple was one of the first I explored when starting with Gambas - it's a very useful learning aid.

cheers.
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Jussi Lahtinen
In reply to this post by Riccardo
Not sure what you are trying to do, but you just keep on adding things to
array B, and then adding the same reference of that array to array A.
Maybe you instead wanted this:

  Dim A As New Variant[]
  Dim B As Variant[]
  Dim i, j As Integer

  For i = 0 To 9
    B = New Variant[]
    For j = 0 To 19
      B.Add(i)
    Next
    A.Add(B)
  Next


Jussi


On Tue, Feb 7, 2017 at 9:28 PM, novae_lithic <[hidden email]> wrote:

> Dim A As New Variant[]
>   Dim B As New Variant[]
>   Dim i, j As Integer
>
>   For i = 0 To 9
>     For j = 0 To 19
>       B.Add((ij))
>     Next
>     A.Add(B)
>   Next /
>
------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: Sort multidimensional array?

Charlie
I played with this problem and came up with the attached.

Hope it helps

sort_second_array.tar

Check out www.gambas.one