❔ 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
this is my list, the object follows
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
take a Func that gives an object and returns the thing to sort by
hm?
btw this already exists in linq
^
what's it called? i haven't seen a method like this
Enumerable.OrderBy Method (System.Linq)
Sorts the elements of a sequence in ascending order.
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 stringit's not complicated, just look at the example provided
i'm on that site, tho i still don't understand how i use that for a list instead of an Array
It doesn't actually have any difference whether it's an array or list
it still tells me that there is no
Artikel.OrderBy
with "Artikel" being the object within the listprobably missing the using
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
no, i'm using linq for other stuff too
that at least does not show a error, lemme try something
Show it 👀
doesn't give me the list, does it save it in that "query" variable?
this is the method at this point
So, what is wrong with it? You're sorting your list and introducing an IEnumerable for it which now contains this sorted list.
so all i'm missing is a little
dummy.ToString()
for that? override already writtenI 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:
nvm override not working for that, if i put that into a Console.WriteLine i get this
Send your foreach method please, I'll just show you how it is supposed to be working.
there is none
So, you got a sorted IEnumerable as a result of your method which has to be displayed properly?
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
You don't need to create an instance of IEnumerable then.
that five times in a switch case so each field has it own possibility?
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
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 elementsikr but let them get the idea of the linq method first
Thank you so much that worked
Make sure to check out more documentation on this method, specifically just to avoid the myriad of duplicate code.
gl
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.