[Gambas-user] Ordering a bi-dimensional string[]...by several columns help wanted.

nando_f at nothingsimple.com nando_f at nothingsimple.com
Mon Dec 3 00:51:50 CET 2018


Another Suggestion:

(1) Where do you get the data when you start the program?
IF it is external and not hard coded in Gambas, then store it outside the program sorted
and you will load it automatically sorted: Country + Town

(2) While you're loading it sorted, create a second string array using that same data
but do an Insertion Sort on the way you want it sorted: Town + Country
This second step is possible even if you have your data hard-coded in Gambas.
Just loop through once and perform an Insertion Sort into this second list.

If you are actually doing Countries and Towns, the list is not infinite and we
shouldn't care of the doubling of data.  If this is something very big, then
there is a clever way to keep only one array, but use a different array which 
points to the data. Such an array is INTEGER[] or LONG[] and point to the row 
of City+Town combination.

And there are other less-obvious ways to do what you want,
but this technique I've used many times and no run-time sorting is needed.

-Nando




--
Open WebMail Project (http://openwebmail.org)


---------- Original Message -----------
From: Charlie Ogier <charlie at cogier.com>
To: user at lists.gambas-basic.org
Sent: Sun, 2 Dec 2018 17:04:11 +0000
Subject: Re: [Gambas-user] Ordering a bi-dimensional string[]...by several columns help
wanted.

> Hi Jorge,
> 
> My solution: -
> 
> Your data is "Country code", "Town" so: -
> 
> **Data[0,0] = "US"* *and**Data[1,0] = "New York"**
> *Data[0,1] = "UK"* and *Data[1,1] = "London"
> ***Etc
> 
> **We want to sort by the "Town" (So we put it first)
> 
> Create a *New String[]* with "Town" <separator> "Country Code" 
> <separator> "Town"
> 
> *SortList.Add(Data[1,0] & "`" & Data[0,0] & "`" Data[1,0])**
> **SortList.Add(Data[1,1] & "`" & Data[0,1] & "`" Data[1,1])**
> *(use a loop)
> 
> Sort the list
> *SortList.Sort()*
> 
> Now rebuild *Data* with the 2nd and 3rd items
> 
> *For iLoop = 0 to ?**
> **    Data[iLoop,0] = Split(SortList[iLoop],"`")[1]***' "UK"*
> **    Data[iLoop,1] = Split(SortList[iLoop],"`")[2] *' "London"*
> **Next*
> 
> See example attached.
> 
> Charlie
> 
> ****
> On 01/12/2018 18:13, Jorge Carrión wrote:
> > The problem:
> >
> > I need order a bi-dimensional array by several columns following de 
> > user's criteria.
> >
> > It is relatively simple to order a two-dimensional array for a column, 
> > not long ago we saw several solutions in this mail list, but what I 
> > need now is to sort by several columns and that each of them can be 
> > ascending or descending.
> >
> > I confess that I have been stuck with the subject for four days, that 
> > I have believed more than once that I had solved it and that in the 
> > end I have given up doing it alone. Does anyone have any idea how to 
> > approach the issue before it drives me crazy?
> >
> > There is a "tricky" solution: grab your two-dimensional array, create 
> > with it a sqlite base and let the sqlite engine solve the issue with 
> > an SQL statement ... but this is not the case (it would force the 
> > project to link with the modules database and it's not always convenient)
> >
> > Waiting for the better minds than mine advice...
> >
> > Best Regards
> >
> > ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
------- End of Original Message -------



More information about the User mailing list