I'd like to compare two objects by multiple properties.
Let's say I have a class called Student, and every student has name and score. My wish is to create a SortedSet(implement a Comparer here), so whenever I add a Student into the collection they will be sorted by their score, and if they have the same score they will be sorted by their name alphabetically.
Java 8 equivalent of this would be:
TreeSet<Student> students = new TreeSet<>(
Comparator.comparing(Student::getScore).thenComparing(Student::getName)
);
Is this possible using Comparer<Student>.Create()
or in any other way?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false .
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
You can use SortedSet
with a custom comparer. Implementation is a little trickier:
var s = new SortedSet<Student>(
Comparer<Student>.Create((a, b) => {
// This code implements comparison by score first
var res= a.Score.CompareTo(b.Score);
// Ties are resolved by name in alphabetic order
return res != 0 ? res : a.Name.CompareTo(b.Name);
})
);
Two quick examples I put together, you could use an IComparer<>
of type Student
. You would then give it the comparisons in the order required, as such:
class Program
{
static void Main(string[] args)
{
var set = new SortedSet<Student>(new StudentComparer());
set.Add(new Student {Name = "Test", Score = 10});
set.Add(new Student { Name = "Tom", Score = 5 });
set.Add(new Student { Name = "Adam", Score = 90 });
set.Add(new Student { Name = "Adam", Score = 85 });
foreach (var setItem in set)
{
Debug.WriteLine($@"{setItem.Name} - {setItem.Score}");
}
/* outputs:
Tom - 5
Test - 10
Adam - 85
Adam - 90
*/
}
}
class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
class StudentComparer : IComparer<Student>
{
public int Compare(Student x, Student y)
{
var result = x.Score.CompareTo(y.Score);
if (result == 0)
{
result = x.Name.CompareTo(y.Name);
}
return result;
}
}
You could also use a normal List, and use Linq:
var students = new List<Student>
{
new Student {Name = "Test", Score = 10},
new Student {Name = "Tom", Score = 5},
new Student {Name = "Adam", Score = 90},
new Student {Name = "Adam", Score = 85}
};
var orderedList = students.OrderByDescending(s => s.Score)
.ThenBy(s => s.Name);
foreach (var student in orderedList)
{
Debug.WriteLine($@"{student.Name} - {student.Score}");
}
/* outputs:
Adam - 90
Adam - 85
Test - 10
Tom - 5
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With