HTML (Solved) Problem with a table and comparing objects in SugarCube.

Spider-Ham

Newbie
May 9, 2017
70
71
Hello.

Well, I'm developing my first game on Twine using SugarCube, but I've reached a point where I'm stuck. Let's see if someone can help me, please.

- In the game there is a leaderboard with three columns, one for the team name, one for the wins and one for the losses. Each cell is assigned a variable, so you can put the results: First Ranked Team Name ($West1), First Ranked Team Wins ($WinsWest1), First Ranked Team Losses ($LossesWest1), etc.

- Then I have established the 22 teams as objects, as follows:

<<set $Boston = {
Name: "Celtics",
Wins: null,
Losses: null,
}>>

- Then I have managed to simulate all the games, so that each team is assigned the wins and losses.

- Now comes the problem: How do I compare the victories of each team, so that they are ordered in the table from highest to lowest, with their names, wins and losses?

Thank you very much in advance ;)
 

averagehtmlenjoyer

Currently enjoying succubi
Game Developer
Jan 8, 2023
211
1,017
You could write a javascript function to sort the array of teams based on the wins field

JavaScript:
//If you already have an array pass it to the function, otherwise you can make one with your objects

var myarray = []

myarray.push({Name: "Celtics", Wins: 8, Losses: 4})
myarray.push({name: "Lakers", Wins: 5, Losses: 5})
myarray.push({name: "Mavericks", Wins: 10, Losses: 3})

var arraySortedByWins = myarray.sort((a, b) => b.Wins-a.Wins);

console.log(arraySortedByWins);
Then return the sorted array and loop over it to create your table

It's also convention to start property names in an object with lowercase.
 
Last edited:
  • Like
Reactions: Spider-Ham

Spider-Ham

Newbie
May 9, 2017
70
71
You could write a javascript function to sort the array of teams based on the wins field

JavaScript:
//If you already have an array pass it to the function, otherwise you can make one with your objects

var myarray = []

myarray.push({Name: "Celtics", Wins: 8, Losses: 4})
myarray.push({name: "Lakers", Wins: 5, Losses: 5})
myarray.push({name: "Mavericks", Wins: 10, Losses: 3})

var arraySortedByWins = myarray.sort((a, b) => b.Wins-a.Wins);

console.log(arraySortedByWins);
Then return the sorted array and loop over it to create your table

It's also convention to start property names in an object with lowercase.
Hello, thank you very much for answering!

Your contribution has been of great help. In the end I have solved the problem in a slightly different way, since I am a newbie to programming and I am still not very good at using JavaScript, but your solution has indicated to me which path I should take to obtain the result that was looking for.

Now I can continue, after a couple of days stuck.

Thank you very much again, it's nice to see how there are people with more knowledge who are willing to help those of us who are starting out in this :D