### Post

2 followers Follow

# wrong sorting

Hi everyone,
with the precious help of Ranier I wrote this code that allows you to summarize the dates of an educational calendar by adding the progressive field. Everything works correctly if the dates are entered progressively on the calendar but if after completing the calendar I need to enter one or more dates before the last one these will have an incorrect progressive value. I ask you for help, this calculation is fundamental to the application I am developing.

Claudio Calabrò

Hey Claudio,

the problem is, that the sorting is done AFTER the calculation of the progressive number.  Means: You must sort first and then calculate the progressive number. There a multiple ways to achieve that, but for most it's to complicated to explain it here. So here's a simply to explain solution (which is not very elegant :) :

Prog += Dur[i] || 0
completely. Then in datesNumber.push() replace the variable Prog with Dur[i] and wrap it in a special character (like you do whith the *):
" | §" + Dur[i] + "§ | "
" | " + Prog + " | "

The next loop can be kept as it is.
Now add a third for-loop (between }; and the table):

var rows = [];
for(var i = 0; i < datesClean.length; i++){
var firstPart = datesClean[i].split("§")[0];
var hrs = datesClean[i].split("§")[1];
var hrs = Number(hrs) || 0;
var thirdPart = datesClean[i].split("§")[2];
var Prog += hrs;
rows.push(firstPart + hrs + thirdPart);
};

And as last line
rows.join("\n")

If you get an error parse the first var hrs and var thirdPart to a string: var hrs = String(datesClean[i].split("§")[1]); and: String(datesClean[i].split("§")[2]).
Sometimes that's necessary cause of some "specialities" of the Podio calculation fields.

Rainer

Hi Ranier,

Thanks for the support, I have applied the new code and it works only if I see all the Items, if I apply the month filter it calculates the progressive value only on the filtered one(I display the values 2, 4 and 6 while I would like to display the values 30, 32 and 34).

The code entered and the results obtained with the whole filter and the month filter:

var Mese = @All of 08CA - Periodo with nulls;
var Filtro = @Filtro;

var Data = @All of Data e Ora Inizio;
var Orario = @All of 08CA - Orario Link with nulls;
var Ore = @All of 08CA - Durata with nulls;
var Dur = @All of 08CA - Durata Num;
var Sede = @All of 08CA - Sede Svolgimento with nulls;
var Arg = @All of 08CA - Argomento with nulls;
var Tipo = @All of Tipologia with nulls;
var Doc = @All of 08CA - Docente/Codocente with nulls;
var Tut = @All of 08CA - Tutor with nulls;
var Prog = 0;

var datesNumber = [];
for(var i = 0; i < Data.length;i++){
if( Filtro == Mese[i] || Filtro == "Tutto"){ datesNumber.push(moment(Data[i]).format("YYYYMMDD") + "*" + Orario[i][0] + " | " + Ore[i][0] + " | \$" + Dur[i] + "\$ | " + Arg[i][0] + " | " + Doc[i][0] + " | " + Tut[i][0]);
}};

var datesSort = datesNumber.sort()
var datesClean = [];
for(var i = 0; i < datesSort.length; i++){
splitDate = datesSort[i].split("*").shift();
splitOther = datesSort[i].split("*").pop();
datesClean.push(moment(splitDate, "YYYYMMDD").format("DD-MM-YYYY") + " | " + splitOther);
};

var rows = [];
for (var i = 0; i <datesClean.length; i ++) {
var firstPart = datesClean[i].split("\$")[0];
var hrs = String(datesClean[i].split("\$")[1]);
var hrs = Number(hrs) || 0;
var thirdPart = String(datesClean[i].split("\$")[2]);
Prog += hrs;
rows.push(firstPart + Prog + thirdPart);
};

"Data | Orario | Ore | Progressivo | Argomento| Docente | Tutor \n" +
"--- | --- | --- | --- | --- | --- | --- \n"+
rows.join("\n")

Month

ALL