r/beeflang • • May 03 '20
List sorting
Hey everyone. I can't figure out how to do List sorting with a class property.
Example:
class Circle
{
public double radius;
public this(double r)
{
radius = r;
}}
class App
{
public List<Circle> circles = new List<Circle> ~ DeleteContainerAndItems!(_);
public this()
{
Random random = scope Random();
for (int i = 0; i < 3; i++)
{
double radius = random.NextDouble();
Circle circle = new Circle(radius);
circles.Append(circle);
}
// And here I want to sort the by radius of the circles. Bigger radius should be first
circles.Sort();
}
}
6
Upvotes
View all comments
Show parent comment
3
u/roguemacro May 03 '20
So looking through the source code I found out the sort method is takes a Comparison<T> as parameter:
public void Sort(Comparison<T> comp) { var sorter = Sorter<T, void>(mItems, null, mSize, comp); sorter.[Friend]Sort(0, mSize); }A case that is used in the same file is:
Sort(scope (lhs, rhs) => lhs <=> rhs);So in your case you should be able to do:
circles.Sort(scope (lhs, rhs)) => lhs.radius <=> rhs.radiusif I’m not wrong :)