sort multidimensional arrays

View: New views
6 Messages — Rating Filter:   Alert me  

sort multidimensional arrays

by Matti-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is there a way to sort an array that has more than one dimension by telling Gambas on which field the sorting shoiuld be done?

For example, if I have the array
Max, 14
Anna, 8
Fabien, 7

I would expect the result of sorting by the first field to be
Anna, 8
Fabien, 7
Max, 14

Instead, when using Array.Sort, I get
7, 8
14, Anna
Fabien, Max

which is useless.

I have a workaround now, reading the array into a single-dimensioned array like
Max~14
Anna~8
Fabien~7

and then sort it, which produces
Anna~8
Fabien~7
Max~14

and then split and read everything back to the original array:
Anna, 8
Fabien, 7
Max, 14

But that just works, it's not really elegant.
Somebody knows a better way?

Thanks
Matti

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Gambas-user mailing list
Gambas-user@...
https://lists.sourceforge.net/lists/listinfo/gambas-user

Re: sort multidimensional arrays

by Fabien Bodard-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

what is the array structure ?

send an exemple please

2009/10/25 Matti <math.eber@...>:

> Is there a way to sort an array that has more than one dimension by telling Gambas on which field the sorting shoiuld be done?
>
> For example, if I have the array
> Max, 14
> Anna, 8
> Fabien, 7
>
> I would expect the result of sorting by the first field to be
> Anna, 8
> Fabien, 7
> Max, 14
>
> Instead, when using Array.Sort, I get
> 7, 8
> 14, Anna
> Fabien, Max
>
> which is useless.
>
> I have a workaround now, reading the array into a single-dimensioned array like
> Max~14
> Anna~8
> Fabien~7
>
> and then sort it, which produces
> Anna~8
> Fabien~7
> Max~14
>
> and then split and read everything back to the original array:
> Anna, 8
> Fabien, 7
> Max, 14
>
> But that just works, it's not really elegant.
> Somebody knows a better way?
>
> Thanks
> Matti
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Gambas-user mailing list
> Gambas-user@...
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Gambas-user mailing list
Gambas-user@...
https://lists.sourceforge.net/lists/listinfo/gambas-user

Re: sort multidimensional arrays

by Benoît Minisini :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Is there a way to sort an array that has more than one dimension by telling
>  Gambas on which field the sorting shoiuld be done?
>
> For example, if I have the array
> Max, 14
> Anna, 8
> Fabien, 7
>
> I would expect the result of sorting by the first field to be
> Anna, 8
> Fabien, 7
> Max, 14
>
> Instead, when using Array.Sort, I get
> 7, 8
> 14, Anna
> Fabien, Max
>
> which is useless.
>
> I have a workaround now, reading the array into a single-dimensioned array
>  like Max~14
> Anna~8
> Fabien~7
>
> and then sort it, which produces
> Anna~8
> Fabien~7
> Max~14
>
> and then split and read everything back to the original array:
> Anna, 8
> Fabien, 7
> Max, 14
>
> But that just works, it's not really elegant.
> Somebody knows a better way?
>
> Thanks
> Matti
>

Not really. The array Sort() method is very simple, as it sorts the contents
of the array linearly, whatever the number of dimensions is.

A workaround is using a Object[], and storing each line of your table inside
by using a dedicated class, or another array.

Sorting an Object[] calls the "_compare" public method on each object. This
way you can sort the array the way you want. Look at the documenation of
Object[].Sort() in the wiki for more details.

In Gambas 3, you don't have to use an Object[], you can use a Variant[][]
instead.

Regards,

--
Benoît Minisini

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Gambas-user mailing list
Gambas-user@...
https://lists.sourceforge.net/lists/listinfo/gambas-user

Re: sort multidimensional arrays

by Matti-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Benoît Minisini schrieb:

> A workaround is using a Object[], and storing each line of your table inside
> by using a dedicated class, or another array.

Hmm. Sounds good, but I can't imagine how to store "each line of your table" into an Object[]
Could you give an example?

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Gambas-user mailing list
Gambas-user@...
https://lists.sourceforge.net/lists/listinfo/gambas-user

Re: sort multidimensional arrays

by Jussi Lahtinen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Perhaps this way:

Class1
sName as String
iNumber as Integer


Dim oo as New Object[]
Dim item as New Class1

item.sName = "Max"
item.iNumber = 14

oo.Add(item)


Jussi


On Tue, Oct 27, 2009 at 00:52, Matti <math.eber@...> wrote:

>
> Benoît Minisini schrieb:
>
>> A workaround is using a Object[], and storing each line of your table inside
>> by using a dedicated class, or another array.
>
> Hmm. Sounds good, but I can't imagine how to store "each line of your table" into an Object[]
> Could you give an example?
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Gambas-user mailing list
> Gambas-user@...
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Gambas-user mailing list
Gambas-user@...
https://lists.sourceforge.net/lists/listinfo/gambas-user

Re: sort multidimensional arrays

by Matti-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think I begin to understand.
I'll try...
...may take some time...
Thanks
Matti

Jussi Lahtinen schrieb:

> Perhaps this way:
>
> Class1
> sName as String
> iNumber as Integer
>
>
> Dim oo as New Object[]
> Dim item as New Class1
>
> item.sName = "Max"
> item.iNumber = 14
>
> oo.Add(item)
>
>
> Jussi
>
>
> On Tue, Oct 27, 2009 at 00:52, Matti <math.eber@...> wrote:
>> Benoît Minisini schrieb:
>>
>>> A workaround is using a Object[], and storing each line of your table inside
>>> by using a dedicated class, or another array.
>> Hmm. Sounds good, but I can't imagine how to store "each line of your table" into an Object[]

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Gambas-user mailing list
Gambas-user@...
https://lists.sourceforge.net/lists/listinfo/gambas-user