Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting summary table #2

Closed
rucksman opened this issue Aug 24, 2022 · 6 comments
Closed

Sorting summary table #2

rucksman opened this issue Aug 24, 2022 · 6 comments

Comments

@rucksman
Copy link

rucksman commented Aug 24, 2022

As mentioned already in your fitplotter repo, I am adapting your wonderful projects (fitalyser and fitplotter) to my needs. I am almost done, but one thing drives me nuts, and I hope to get some help from you.

I simply want to have the summary table sorted by date in descending order. Therefore I delete the table every time there is a new activity and changed the code in utilities/summaryTable.js as follows. The idea is to read the files in activitiesFolder and sort them by creation date in the desired order before parsing them:

fs.readdir(activitiesFolder, (err, files) => {
  if (err) {
    console.log("\nDoes not exist: \"" + activitiesFolder + "\n\nCheck path to FIT files\n");
    return;
  } else {
    //START NEW CODE
    files.sort(function(a, b) {
      return fs.statSync(activitiesFolder + "/" + b).birthtime.getTime() - fs.statSync(activitiesFolder + "/" + a).birthtime.getTime();
    });
    // END NEW CODE
    files.forEach(file => {
      if (file.slice(-4) === fileExt) {

But the table is still not sorted by date in descending order. The funny thing is, that if I am starting summaryTable.bat again and agan after deleting the table, the newly generated table is always sorted in a different way, which I do not understand at all.

Can you give me a hint what I am doing wrong here?

node.js and javascript is not my homeground, so maybe my question is silly, but I am always eager to learn. Thank you!

@rucksman
Copy link
Author

Just to add one more thing: If I console.log the array "files" before and after the sort function, there is a difference, and the array after the sort function is in the correct order. But the csv file is always different, and there seems to be no pattern for the order.

@karaul
Copy link
Owner

karaul commented Aug 24, 2022

I think the problem is that the node.js is tricky by working asynchroniously. It probably explains why "the table is still not sorted by date in descending order. The funny thing is, that if I am starting summaryTable.bat again and agan after deleting the table, the newly generated table is always sorted in a different way". That is, in the asynchronuios mode, one should non expect that the commands are executed in the strict sequence, the command can start earlier than a command before, it's a nightmare for programmers. It is my suggestiom, and honestly, I have no idea about sorting with the node.js promises

https://wanago.io/2018/04/09/explaining-promises-and-callbacks-while-implementing-a-sorting-algorithm/

@rucksman
Copy link
Author

Just investigating further: Until this line (126) in utilities/summaryTable.js the "file" is correct:

fs.readFile(activitiesFolder + "/" + file, function (err, content) {

But if I console.log "file" after this line, "file" is suddenly different. How come?

@rucksman
Copy link
Author

Ended up with a very awkward "solution": At the end of summaryTable.js I added some code to open the CSV file, sort the entries and write the CSV file again. If I find the time I will probably think about a better solution ...

@karaul
Copy link
Owner

karaul commented Aug 27, 2022

s I added some code to open the CSV file, sort the entries and write the CSV file again

if it's so, the reason is certainly because of asynchronuios features. Anyway, whatever working is better than anything not.

@rucksman
Copy link
Author

In the meantime I implemented a better solution. I changed the code in summaryTable.js from approx. line 173 on so that the array is already in the desired order and table.csv is only written once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants