L'ordinamento che viene eseguito è quello alfabetico, che con i numeri può creare qualche problema:
Alcuni programmatori, soprattutto in altri linguaggi, preferiscono risolvere il problema mettendo i numeri i numeri tutti con lo stesso numero di caratteri: 01, 02, 03, .., 09, 10, 11, 12. Pessima idea!
Si può chiedere a livecode di usare l'ordinamento numerico, vedremo nei seguenti passaggi come fare.
Il codice che vedrete va inserito nel gruppo del datagrid, non in altri posti (come il template), quindi quando vi si aprirà la finestra del codice, essa sarà bianca e immacolata; altrimenti state mettendo il codice nel posto sbagliato.
Il codice è il seguente:
on SortDataGridColumn pColumn switch pColumn case "Col 1" #questo avverra solo se è la colonna che ci interessa, in questo caso "col 1" put the dgData of me into theDataA #ricordiamo che in questo array speciale: keys= righe set the itemdelimiter to tab # ci servirà dopo repeat for each key theIndex in theDataA put theIndex & tab & theDataA[theIndex][pColumn] & cr after theData end repeat delete the last char of theData #rimuove l'ultimo a capo #adesso ordiniamo i dati (secondo item) put the dgColumnSortDirection[pColumn] of me into theSortDirection if theSortDirection is "ascending" then sort lines of theData ascending numeric by item 2 to -1 of each # else sort lines of theData descending numeric by item 2 to -1 of each end if ## costruimo il testo che dice l'ordine delle righe put empty into theIndexSequencing repeat for each line theLine in theData put item 1 of theLine & comma after theIndexSequencing end repeat delete the last char of theIndexSequencing #Ordiniamo effetivamente le righe set the dgIndexes of me to theIndexSequencing ## evidenziamo la colonna HiliteAndStoreSortByColumn pColumn break default #altrimenti è un'altra colonna e tutto si svolge come al solito pass SortDataGridColumn end switchend SortDataGridColumn
Adesso la colonna con i numeri si ordina bene:
Ora esaminiamo un caso leggermente più difficile, la colonna contiene numeri che esprimo soldi; quindi c'è il simbolo della valuta davanti. Anche qui si usa lo stesso sistema e leviamo il simbolo della valuta nel testo temporaneo theData che usiamo per ordinare i dati:
on SortDataGridColumn pColumn switch pColumn case "Col 1" #questo avverra solo se è la colonna che ci interessa, in questo caso "col 1" put the dgData of me into theDataA #ricordiamo che in questo array speciale: keys= righe set the itemdelimiter to tab # ci servirà dopo repeat for each key theIndex in theDataA put theIndex & tab & theDataA[theIndex][pColumn] & cr after theData delete char 1 of item 2 of the last line of thedata end repeat delete the last char of theData #rimuove l'ultimo a capo #adesso ordiniamo i dati (secondo item) put the dgColumnSortDirection[pColumn] of me into theSortDirection if theSortDirection is "ascending" then sort lines of theData ascending numeric by item 2 to -1 of each # else sort lines of theData descending numeric by item 2 to -1 of each end if ## costruimo il testo che dice l'ordine delle righe put empty into theIndexSequencing repeat for each line theLine in theData put item 1 of theLine & comma after theIndexSequencing end repeat delete the last char of theIndexSequencing #Ordiniamo effetivamente le righe set the dgIndexes of me to theIndexSequencing ## evidenziamo la colonna HiliteAndStoreSortByColumn pColumn break default #altrimenti è un'altra colonna e tutto si svolge come al solito pass SortDataGridColumn end switchend SortDataGridColumn
e questo è il risultato:



Nessun commento:
Posta un commento