Sort a list by multiple attributes?
A key can be a function that returns a tuple:
s = sorted(s, key = lambda x: (x[1], x[2]))
Or you can achieve the same using itemgetter
(which is faster and avoids a Python function call):
import operator
s = sorted(s, key = operator.itemgetter(1, 2))
And notice that here you can use sort
instead of using sorted
and then reassigning:
s.sort(key = operator.itemgetter(1, 2))
Ordering a list in-place by two properties
The answer you linked to is correct. The key in sorting by multiple values is that the secondary property only matters if the primaries are equal. A psuedocode implementation of your sort comparison might be:
compare x and y position
if they differ, return order
else compare name, return order
On the Sort
method, the code following the (x,y)=>
must return 0 if the items are equal, a negative number if the first should be before the second, and a positive number if the second should come before the first. The CompareTo
method will return -1, 0, or 1 based on these cases and its arguments. Since you need to compare two different properties, you need two calls to CompareTo
. If you decided to add them together, you could have a case like this:
- x.position < y.position (compare returns -1)
- x.name > y.name (compare returns 1)
- result 0, items are considered equal, where your rules
clearly say x should come first in this case.
To solve this issue, we need to make sure the Name comparison only matters when the positions are equal. Since CompareTo
only returns -1, 0, or 1, if we multiply the position result by 2 (or any larger number) then the Name comparison will only change the result if the positions are equal. (Because -2 + 1 = -1
and 2 - 1 = 1
)
So using the method in your original linked answer, your code would be something like:
list.Sort((x, y) =>
2 * x.Position.CompareTo(y.Position)
+ x.Name.CompareTo(y.Name));
How to Sort a ListT by a property in the object
The easiest way I can think of is to use Linq:
List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();
How to sort list items based on multiple attributes, while the last priority is alphabet?
Just put the name as the last element of the tuple:
teams = sorted([Spain, Portugal, Iran, Morocco], key = lambda x : (x.points, x.wins, x.name))
Sort and group a list of objects by multiple properties and conditions
Tasks
is a collection of instances of a class. Create a calculated property in that class called OrderPriority
.
public byte OrderPriority
{
get
{
if (HighPriority && !Completed)
return 1;
if (Priority && !Completed)
return 2;
// and so forth
}
}
Then your LINQ could look like:
Tasks.OrderBy(x => x.OrderPriority).ThenBy(x => x.Date).ToList();
Kotlin: Sort list by mutliple attributes
Use sortWith
and a custom comparator
val list = mutableListOf("abab", "abcd", "aaa")
list.sortWith(compareBy(String::length).thenBy { it.count { char -> char == 'a'} })
Here you can see documentation for all functions that will help you create a new comparator:
kotlin.comparisons
Related Topics
C# Encryption to PHP Decryption
Update Requires a Valid Updatecommand When Passed Datarow Collection with Modified Rows
Opening Process and Changing Window Position
Why Does the Async Keyword Exist
C# Open File, Path Starting with %Userprofile%
Equality Comparison Between Multiple Variables
Getting Relative Virtual Path from Physical Path
Wpf: the Name Does Not Exist in the Namespace
The Remote Server Returned an Unexpected Response: (413) Request Entity Too Large
Why Is Double.Nan Not Equal to Itself
Is There an Entity Framework 7 Database-First Poco Generator