Sorting structs?

Stanislav Blinov stanislav.blinov at gmail.com
Wed Jan 29 01:49:10 PST 2014


On Wednesday, 29 January 2014 at 09:33:50 UTC, Boomerang wrote:

> I would appreciate it if you explained your approach and gave
> source code illustrating it. Thanks!

This can be solved without sorting in reverse or overloading 
anything in C++ (with a predicate version of sort() algorithm), 
and in D too. Take a look at this code:

http://dpaste.dzfl.pl/2d0dd6f953dc

the std.algorithm.sort() takes an optional predicate (as a 
template argument) to resolve the order of elements. In this 
case, it is specified as an anonymous function

(a,b) => a.grade > b.grade

You could alternatively specify it as a string:

sort!"a.grade > b.grade"(students);

or like this:

sort!q{a.grade > b.grade}(students);


More information about the Digitalmars-d-learn mailing list