Page 1 of 1
sorting array on two values
Posted: Fri Oct 09, 2020 9:16 am
by marksmithhfx
Even though I use the following code, I don't fully understand how works ( I got it from the LC lesson "How do I sort an array") and just modified it for use. However, now I would like to sort on two values: category and dgOrder. Does anyone know how I could modify this to do that?
Code: Select all
function sortArray @pArray
local tSortedArray
local tNewRow, tNewOrder
# fetch the keys and sort them using the array entry values
get the keys of pArray
sort lines of it numeric by pArray[each]["dgOrder"]
put it into tNewOrder
put 0 into tNewRow
repeat for each line tLine in tNewOrder
add 1 to tNewRow
#... create the new array
put pArray[tLine]["RowID"] into tSortedArray[tNewRow]["RowID"]
put pArray[tLine]["complete"] into tSortedArray[tNewRow]["complete"]
put pArray[tLine]["itemDate"] into tSortedArray[tNewRow]["itemDate"]
put pArray[tLine]["category"] into tSortedArray[tNewRow]["category"]
put pArray[tLine]["purgeDate"] into tSortedArray[tNewRow]["purgeDate"]
put pArray[tLine]["dgOrder"] into tSortedArray[tNewRow]["dgOrder"]
end repeat
return tSortedArray
end sortArray
Thanks
Mark
Re: sorting array on two values
Posted: Fri Oct 09, 2020 5:08 pm
by jacque
Add a second sort command after the first one:
Code: Select all
sort lines of it numeric by pArray[each]["dgOrder"]
sort lines of it by pArray[each]["category"]
That adds the category sort after the first one, and the rest of the handler can run as-is.
Re: sorting array on two values
Posted: Fri Oct 09, 2020 6:28 pm
by SparkOut
This kind of magic is thanks to the nondestructive sorting paradigm. Or is it actually just magic? I think it's magic.
Re: sorting array on two values
Posted: Fri Oct 09, 2020 8:04 pm
by FourthWorld
Associative arrays are hashes, and as such have no inherent sense of order.
What are you aiming to do?
Re: sorting array on two values
Posted: Fri Oct 09, 2020 8:12 pm
by marksmithhfx
jacque wrote: ↑Fri Oct 09, 2020 5:08 pm
Add a second sort command after the first one:
Code: Select all
sort lines of it numeric by pArray[each]["dgOrder"]
sort lines of it by pArray[each]["category"]
That adds the category sort after the first one, and the rest of the handler can run as-is.
Very cool. Thanks Jacque
Re: sorting array on two values
Posted: Fri Oct 09, 2020 9:46 pm
by marksmithhfx
FourthWorld wrote: ↑Fri Oct 09, 2020 8:04 pm
Associative arrays are hashes, and as such have no inherent sense of order.
What are you aiming to do?
Hi Richard,
Thanks for asking. I was maintaining a list in the new DG2 and as you know you can re-order items. Very handy, but of course if you save it out to a database as I was doing you don't want to be reordering the database, so you store the row item order when you save the records. That way even if they were stored as 1,2,3,4,5 and then re-ordered to 5,4,3,2,1 the order in the DB remains but the order in the dg is reversed. So on reload, sort the elements into the desired order.
What happens if you decide to add the concept of categories to your list? Or different lists for different categories. It would be fine if everything was always categorized but I had started out without categories, and still wanted that ability. A bit like saying "show me my lists by category, then forget that and show me all the items in the list". What order should that be in? Well after looking at the mishmash that resulted I decided that if items appeared on the list grouped by category, and then within each category in the same order they were in before, that would probably be best.
I can't swear Jacques solution has solved the problem for all lists. But for a simple list I created to test with, it miraculously put them into the right groups and the right order. Of course, with this degree of complexity things have a habit of getting out of control pretty quickly, and just when you think you have the problem nailed, another one pops up. In short, I still have more testing to do but it looks promising.
Your question was timely. In revisiting what I was doing I realized there was no visual way to differentiate the categories when they were presented in an uncategorized fashion, and that colour coding the categories might help. I'm not sure that will work (changing colours on the fly) but I'll try working on it this weekend.
Mark
Re: sorting array on two values
Posted: Fri Oct 09, 2020 9:52 pm
by FourthWorld
Does the DG's built-in sorting not cover what you're looking for?
http://lessons.livecode.com/m/datagrid/ ... y-a-column
Re: sorting array on two values
Posted: Sat Oct 10, 2020 4:21 pm
by marksmithhfx
No, the kind of sorting (more like ordering) I was looking for is more defined by an assessment of the content. For example, lets say you build a list of the steps you need to paint your house...
consultant on color options
define quantities of paint needed
decide if price is a significant factor
seek qualified estimates
purchase paint
are more painters needed or is this diy?
schedule a sequence of rooms to be painted
etc
Now, it might be that you want to rearrange some of those items later on. Say, decide if price is a significant factor before deciding on color, so with the new DG2 you can just drag the row from position 3 to 1. Now, in the db the items don't get re-ordered. Instead you just put a 1 in the order field for that item (decide if price...), and all previous items are shifted down (actually, you don't even have to do that... with the new dg2 you can just UPDATE each record in the db with "the dgLine of me"). When you reload, you just sort by the "order" field.
It gets a bit hairier when you start introducing the concept of "projects" or "categories" into the list. A list that is already segmented by time. But I'll let you distill that one. Eventually it is hoped this will see the light-of-day in the app Store. But still a ways to go yet. It is a tad amazing how complicated these projects can become.
But a big shout out to everyone here who has been answering my questions. You have really helped in overcoming some substantial hurdles. Very grateful to all of you for the support this community provides.
So, to get back to your idea of using column header sorting. I suppose if the dgOrder field was explicit (it's not) and the category field appeared on the list (it doesn't) you would be able to do it with column heading sorts. However, once you see it you'll realize those items would be both distracting and fairly undesirable. At least I hope I've made the right call.
Mark
Re: sorting array on two values
Posted: Sat Oct 10, 2020 5:39 pm
by FourthWorld
Options built into the DataGrid aren't limited to the column header UI. They can be done programmatically, using the property trigger per the example I linked to above.
I see no harm in writing your own, but in my reading it appears that the custom solutions discussed here offer variants of what the DG's API offers, so using what's there already would seem useful to save a little development time.
Did I miss something?
Re: sorting array on two values
Posted: Sat Oct 10, 2020 8:46 pm
by marksmithhfx
FourthWorld wrote: ↑Sat Oct 10, 2020 5:39 pm
using what's there already would seem useful to save a little development time.
Did I miss something?
No, probably more likely I did. When, historically, I first went looking for a solution to sort the grid I had already adopted the example that defines the grid by reading the data into a db cursor and then converts it to an array (set the dgData of group "DataGrid 1" to theDataGridArray). So I was looking for an example of how to sort an array, which, there is a lesson on (the one that starts this message thread). The other thing that probably did not lead me down the right path, or at least encourage me to look at the solution you posted more carefully is because my dg is form based and so there is no column pane to select. Mind you, I think your "program it from code" would still work just fine because I already used some of the other columns in scripts (for example, the sorting script), it would probably just be a bit more fiddly than the example you posted indicates. For example, not sure how I would modify "set the dgProps["sort by column"] of group "DataGrid" to "dgOrder" to include Category as well?
Mark
Re: sorting array on two values
Posted: Sat Oct 10, 2020 10:31 pm
by FourthWorld
If the data is tabular you can get and set a DG's dgText property, which is tab-delimited. A couple quick sorts on that list, tuck it back into the object, and you're done.
Re: sorting array on two values
Posted: Sun Oct 11, 2020 1:26 pm
by marksmithhfx
FourthWorld wrote: ↑Sat Oct 10, 2020 10:31 pm
If the data is tabular you can get and set a DG's dgText property, which is tab-delimited. A couple quick sorts on that list, tuck it back into the object, and you're done.
Ok, good to know. Things are working splendidly at the moment so I daren't fiddle too much
M
Re: sorting array on two values
Posted: Sun Oct 11, 2020 2:29 pm
by dunbarx
Not bragging, but I always extract either the dgData or the dgText, do whatever back "inside" LC, and then reload the DG.
The "not bragging" thing is simply due to laziness in not learning the DG API.
Craig
Re: sorting array on two values
Posted: Sun Oct 11, 2020 5:00 pm
by FourthWorld
dunbarx wrote: ↑Sun Oct 11, 2020 2:29 pm
Not bragging, but I always extract either the dgData or the dgText, do whatever back "inside" LC, and then reload the DG.
The "not bragging" thing is simply due to laziness in not learning the DG API.
The DG API is rich enough that it can support a wide range of development styles, but FWIW I tend to do the same as you.
In general I tend to think of GUI elements as display containers, separate from storage and separate from the business logic that negotiates between the two.
There's lots of ways to work and I wouldn't claim any is necessarily "best"; I tend not to believe in one-size-fits-all "best" for anything in life that has so many options.
But for the work I do, I've found that separating code, UI, and data has helped me be more flexible with changing any one of those fewer implications for the other two.