Im new to programming and this might be some rookie problem that im having, but id appreaciate a hand.
void Sort()
{
List<Lag> lagen = new List<Lag>() { AIK, DIF, MLM, BJK };
List<Lag> sorted = lagen.OrderByDescending(x => x.poäng)
.ThenByDescending(x => x.målSK)
.ThenByDescending(x => x.mål)
.ToList();
Print(sorted);
}
This is my sorting function, where i take one list, and turn it into a list called "sorted".
What i want to do now is let the user pick one of the objects in the sorted list by entering a number. This far iw written the following code. Where int hemlag, and int bortlag are objects in the sorted list.
And now i want to change the value of "Lag hemmalag" & "Lag bortalag" depending on what numbers (what team) the user put in.
So for example, if the user input "int hemlag" is 2.. i want hemmalag = to be the 2nd object in the list "sorted". And now i run into a problem. Cause i cant access that sorted list from this function.
My theories are that it might have to do something with returning that list from the sorting function, but i have not yet found a way to do that…
void ChangeStats(int hemlag, int bortlag, int mål, int insläpp)
{
Sortera();
Lag hemmalag = AIK;
Lag bortalag = AIK;
if (hemlag == 1) { ; }
if (hemlag == 2) { hemmalag = DIF; }
if (hemlag == 3) { hemmalag = MLM; }
if (hemlag == 4) { hemmalag = BJK; }
if (bortlag == 1) { bortalag = AIK; }
if (bortlag == 2) { bortalag = DIF; }
if (bortlag == 3) { bortalag = MLM; }
if (bortlag == 4) { bortalag = BJK; }
hemmalag.mål += mål;
hemmalag.insläppta += insläpp;
bortalag.insläppta += mål;
bortalag.mål += insläpp;
if (mål > insläpp)
{
hemmalag.poäng += 3;
hemmalag.vinster++;
hemmalag.spel++;
bortalag.förlorade++;
bortalag.spel++;
}
if (mål < insläpp)
{
bortalag.poäng += 3;
bortalag.vinster++;
bortalag.spel++;
hemmalag.förlorade++;
hemmalag.spel++;
}
if (mål == insläpp)
{
bortalag.lika++;
bortalag.poäng++;
hemmalag.lika++;
bortalag.poäng++;
}
Console.WriteLine("Stats changed");
Console.WriteLine("---");
Save();
Sortera();
}
Help appreciated, cheers!
>Solution :
Here is my example on the global scope list. I’m not 100% this can sort the issue but I’m confident.
class Example {
private List<Lag> Lagen {
get;
set;
} // Global Scope - Make it public if you need to access it from another
// class.
public Example() {
this.Lagen = new List<Lag>{AIK, DIF, MLM,
BJK}; // Assign intial values on class execution
}
void Sort() {
// Everything else will be the same but now you can access it from anywhere
// within the class
List<Lag> sorted = Lagen.OrderByDescending(x => x.poäng)
.ThenByDescending(x => x.målSK)
.ThenByDescending(x => x.mål)
.ToList();
Print(sorted);
}
}