Hey! i am wondering how I can sort a class array that contains diffrent typs of data structuers?
for example, an Array where each element in the array contains string’s, Double’s or int’s .
And then sort them for each category based on what the user wants?
Class Cat{
string Name;
double weight;
int Age;
}
Cat[] Cats = new Cat[3]
Cats[0] = “bob”, 11.1, 2
Cats[1] = “Carl”, 7.4, 14
Cats[2] = “Missy”, 8.4, 1
cats.OrderBy(c => c.Name);
As for “what the user wants” that’s up to you and depends on how you’re receiving that input.
thanks you my hero! this is exactly what i have been looking for! i do know how to use this for my code!
Another way of doing this would be to use the IComparer interface. https://docs.microsoft.com/en-us/dotnet/api/system.collections.icomparer?view=net-5.0 You can make the 3 different cat comparers and call the collection sort method, using your IComparers you just made.
edit: Here is an example. https://pastebin.com/05xnvH5k
I just love how reddit is more noob friendly! thanks for help! as soon as i asked anything on stackoverflow everyone starded hating:P your tip might help me even more
ooh, 1 problem though! this is using ‘list’, i need array 😛 dose it work anyway?
I quickly put this together. The important part is cats.OrderBy(c => c.Age).ToArray()
. It’s using the Linq extension method OrderBy. This will only work if you have using System.Linq;
at the top of your code file. The c => c.Age
bit is basically choosing the Age property of a cat.
Now, how to do in for user input might be something that you can figure out yourself now. If not, maybe ask a more specific question about the specific way you receive input and the format, etc. Your question is too general in order to give you a specific answer to how to do it based on user input.
thanks you my hero! this is exactly what i have been looking for! i do know how to use this for my code!
Tagging OP u/quepopo for instruction because they’re new.
The answer here is correct as far as it goes but one thing you should be aware of is that this answer can be wasteful in terms of memory usage for large lists/arrays. Inside the Main
method, there are three separate, distinct copies of the cats
array when in reality there need only be one (at least for this example).
The OrderBy
Linq extension method returns an IEnumerable
rather than a concrete list (technically it’s an IOrderedEnumerable
). You can turn that IEnumerable
into a list or array by using ToList
or ToArray
as you desire, but unless you actually need that list, it’s quite often better to just leave the IEnumerable
as it is.
Leaving the enumerable as a Linq query allows you to easily chain together more clauses and filters so that at the end, when you *do* iterate over the list or turn it into a concrete list or array, then you only do it once.
Note that this does come at a cost if speed is a large concern in your application. You might *need* to sort your list in place with the .Sort
method and an IComparable
so that you’re not recomputing sort indexes all the time.
It’s always a trade-off. Convenience vs speed vs resource consumption. It’ll be up to you, the author, to determine which is important for your application.
C# devs
null reference exceptions