Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort objects in List by properties on the object

I have a List of objects in C#. All the objects contain properties code1 and code2 (among other properties). The list of objects is in no particular order. I need to sort the list of objects by their code1 and code2 properties.

Example:

List -> object = id, name, code1, code2, hours, amount.

Example code 1 = 004
Example code 2 = 001, 002, 003, 004, 016
Example code 1 = 005
Example code 2 = 001, 002, 003, 004

So after the sort I would want the objects in the following order

004 001
004 002
004 003
004 005
004 016
005 001
005 002
005 003
005 004
like image 699
Baxter Avatar asked Apr 04 '12 14:04

Baxter


People also ask

How do you sort a list of objects based on an attribute of the objects?

A simple solution is to use the list. sort() function to sort a collection of objects (using some attribute) in Python. This function sorts the list in-place and produces a stable sort. It accepts two optional keyword-only arguments: key and reverse.

How do you sort a list of objects based on an attribute of the objects in Java?

In the main() method, we've created an array list of custom objects list, initialized with 5 objects. For sorting the list with the given property, we use the list's sort() method. The sort() method takes the list to be sorted (final sorted list is also the same) and a comparator.

How can we sort a list of objects?

sort() method to sort a list of objects using some examples. By default, the sort() method sorts a given list into ascending order (or natural order). We can use Collections. reverseOrder() method, which returns a Comparator, for reverse sorting.

How do you sort a list by a property in C?

Use the OrderBy Method to Sort a List by a Property in the Object in C We will use the built-in function OrderBy to sort a list by a property in the object. It is LINQ’s method. This method creates a new list sorted by the given property.

How to sort a list of objects in a list?

If you need to sort the list in-place then you can use the Sort method, passing a Comparison<T> delegate: objListOrder.Sort((x, y) => x.OrderDate.CompareTo(y.OrderDate)); If you prefer to create a new, sorted sequence rather than sort in-place then you can use LINQ's OrderBy method, as mentioned in the other answers.

How does Sort-Object sort objects based on properties?

Specifies the property names that Sort-Object uses to sort the objects. Wildcards are permitted. Objects are sorted based on the property values. If you do not specify a property, Sort-Object sorts based on default properties for the object type or the objects themselves. Use commas to separate multiple properties.

How do you sort multiple properties in Python?

Multiple properties can be sorted in ascending order, descending order, or a combination of sort orders. When you specify multiple properties, the objects are sorted by the first property. If multiple objects have the same value for the first property, those objects are sorted by the second property.


1 Answers

You could use linq extensions (leaving the original list unsorted):

var sorted = theList.OrderBy(o => o.code1).ThenBy(o => o.code2);

To replace the original list with a sorted one, make a slight amendment (not very efficient, it creates a new list):

theList = theList.OrderBy(o => o.code1).ThenBy(o => o.code2).ToList();

This assumes that your list is of the correct type, something like:

List<MyClass> theList = new List<MyClass>();

And not a list of objects, in which case you would need to make use of .Cast<>() or .OfType<>().

like image 142
Adam Houldsworth Avatar answered Oct 15 '22 10:10

Adam Houldsworth