C
C#3y ago
Ash_

❔ How do i sort a list of objects by multiple possible items

Hey, i'm trying to write a method to return a list of objects by letting the user choose which item of the object they want it sorted by, code follows
33 Replies
Ash_
Ash_OP3y ago
public static List<Artikel> Auftragsliste = new();
public static List<Artikel> Auftragsliste = new();
this is my list, the object follows
public string Gtin;
public string Artikelnummer;
public string Bezeichnung;
public string Warengruppe;
public double Preis;
public string Gtin;
public string Artikelnummer;
public string Bezeichnung;
public string Warengruppe;
public double Preis;
I want the user to be able to choose by which of these items they want the list sorted by, does someone know what i could do? also if there is any relevant code missing tell me and i'll try to upload it
sibber
sibber3y ago
take a Func that gives an object and returns the thing to sort by
Ash_
Ash_OP3y ago
hm?
sibber
sibber3y ago
btw this already exists in linq
plam
plam3y ago
^
Ash_
Ash_OP3y ago
what's it called? i haven't seen a method like this
Ash_
Ash_OP3y ago
that looks complicated... i'd love to be able to do something like GetList(List,1) and have it output sorted by the first item, here a string
Anu6is
Anu6is3y ago
it's not complicated, just look at the example provided
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}

public static void OrderByEx1()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };

IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age); //this is the part you are interested in

foreach (Pet pet in query)
{
Console.WriteLine("{0} - {1}", pet.Name, pet.Age);
}
}

/*
This code produces the following output:

Whiskers - 1
Boots - 4
Barley - 8
*/
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}

public static void OrderByEx1()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };

IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age); //this is the part you are interested in

foreach (Pet pet in query)
{
Console.WriteLine("{0} - {1}", pet.Name, pet.Age);
}
}

/*
This code produces the following output:

Whiskers - 1
Boots - 4
Barley - 8
*/
Ash_
Ash_OP3y ago
i'm on that site, tho i still don't understand how i use that for a list instead of an Array
plam
plam3y ago
It doesn't actually have any difference whether it's an array or list
public class Foo
{
public string Name { get; }
public int Age { get; }

public Foo(string name, int age)
{
Name = name;
Age = age;
}

public override string ToString() => "[" + Name + ", " + Age + "]";
}

public static class Program
{
public static void Main()
{
Foo[] foos =
{
new Foo("First", 27), new Foo("Third", 21),
new Foo("Second", 24), new Foo("Fourth", 30)
};

Foo[] sorted = foos.OrderBy(foo => foo.Age).ToArray(); // any field instead of its Age

foreach (var it in sorted) Console.WriteLine(it);
}
}

*/
// Before:
[First, 27]
[Third, 21]
[Second, 24]
[Fourth, 30]

// After:
[Third, 21]
[Second, 24]
[First, 27]
[Fourth, 30]
*/
public class Foo
{
public string Name { get; }
public int Age { get; }

public Foo(string name, int age)
{
Name = name;
Age = age;
}

public override string ToString() => "[" + Name + ", " + Age + "]";
}

public static class Program
{
public static void Main()
{
Foo[] foos =
{
new Foo("First", 27), new Foo("Third", 21),
new Foo("Second", 24), new Foo("Fourth", 30)
};

Foo[] sorted = foos.OrderBy(foo => foo.Age).ToArray(); // any field instead of its Age

foreach (var it in sorted) Console.WriteLine(it);
}
}

*/
// Before:
[First, 27]
[Third, 21]
[Second, 24]
[Fourth, 30]

// After:
[Third, 21]
[Second, 24]
[First, 27]
[Fourth, 30]
*/
Ash_
Ash_OP3y ago
it still tells me that there is no Artikel.OrderBy with "Artikel" being the object within the list
Anu6is
Anu6is3y ago
probably missing the using
plam
plam3y ago
You have to use the LINQ method on the list of your objects, not on a single one. additionally add the using, that's what Anubis said
Ash_
Ash_OP3y ago
no, i'm using linq for other stuff too that at least does not show a error, lemme try something
plam
plam3y ago
Show it 👀
Ash_
Ash_OP3y ago
doesn't give me the list, does it save it in that "query" variable?
public static void GetList(int sortby)
{
switch (sortby)
{
default:
Console.WriteLine("Bitte geben Sie eine gültige Kategorie von 1-5 an");
break;
case 1:
IEnumerable<Artikel> dummy = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Gtin);
break;
case 2:
IEnumerable<Artikel> dummy1 = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Artikelnummer);
break;
case 3:
IEnumerable<Artikel> dummy2 = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Bezeichnung);
break;
case 4:
IEnumerable<Artikel> dummy3 = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Warengruppe);
break;
case 5:
IEnumerable<Artikel> dummy4 = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Preis);
break;
}

}
public static void GetList(int sortby)
{
switch (sortby)
{
default:
Console.WriteLine("Bitte geben Sie eine gültige Kategorie von 1-5 an");
break;
case 1:
IEnumerable<Artikel> dummy = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Gtin);
break;
case 2:
IEnumerable<Artikel> dummy1 = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Artikelnummer);
break;
case 3:
IEnumerable<Artikel> dummy2 = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Bezeichnung);
break;
case 4:
IEnumerable<Artikel> dummy3 = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Warengruppe);
break;
case 5:
IEnumerable<Artikel> dummy4 = Artikelliste.Auftragsliste.OrderBy(artikel => artikel.Preis);
break;
}

}
this is the method at this point
plam
plam3y ago
So, what is wrong with it? You're sorting your list and introducing an IEnumerable for it which now contains this sorted list.
IEnumerable<Artikel> dummy = Auftragsliste.OrderBy(artikel => artikel.Gtin);
IEnumerable<Artikel> dummy = Auftragsliste.OrderBy(artikel => artikel.Gtin);
Ash_
Ash_OP3y ago
so all i'm missing is a little dummy.ToString() for that? override already written
plam
plam3y ago
I mean like.. ToString doesn't impact your result code that's just a way of writing it in console. Just foreach your IEnumerable and display it in any way you want:
Either its an:
(override of ToString() or Console.WriteLine("{0} - {1}", obj.Field, obj.Field);
Either its an:
(override of ToString() or Console.WriteLine("{0} - {1}", obj.Field, obj.Field);
Ash_
Ash_OP3y ago
nvm override not working for that, if i put that into a Console.WriteLine i get this
System.Linq.OrderedEnumerable`2[Warenspeichersystem.Artikel,System.String]
System.Linq.OrderedEnumerable`2[Warenspeichersystem.Artikel,System.String]
plam
plam3y ago
Send your foreach method please, I'll just show you how it is supposed to be working.
Ash_
Ash_OP3y ago
there is none
plam
plam3y ago
So, you got a sorted IEnumerable as a result of your method which has to be displayed properly?
Ash_
Ash_OP3y ago
i guess? as i said all this have to do is to display it into the console, i don't need it to be saved somewhere
plam
plam3y ago
You don't need to create an instance of IEnumerable then.
foreach (var it in Auftragsliste.OrderBy(artikel => artikel.FIELD)) {
Console.WriteLine(it);
}
foreach (var it in Auftragsliste.OrderBy(artikel => artikel.FIELD)) {
Console.WriteLine(it);
}
Ash_
Ash_OP3y ago
that five times in a switch case so each field has it own possibility?
plam
plam3y ago
basically just change the specific field and override ToString() method in your Artikel class it's not a great way of implementing it but you'll be able to rewrite it after
cap5lut
cap5lut3y ago
that would work but u would have a ton of duplicate code, i would store the Artikelliste.Auftragsliste as IEnumerable<Artikel> before the switch, then in the case branches simply do dummy = dummy.OrderBy(...) and after the switch use a foreach loop to print the elements
plam
plam3y ago
ikr but let them get the idea of the linq method first
Ash_
Ash_OP3y ago
Thank you so much that worked
plam
plam3y ago
Make sure to check out more documentation on this method, specifically just to avoid the myriad of duplicate code. gl
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server