1

I need to convert this code from java to c#..

points = points.sort(function(a, b) {
    return a.angle - b.angle;
}); 

I tried this

for (var i = 0; i < points.Count-1; i++)
{
    for (var j = i + 1; j < points.Count; j++)
    {
        if (points[i].angle > points[j].angle)
        {
            punct aux = points[i];
            points[i] = points[j];
            points[j] = aux;
        }
    }
}

But it didn't worked.. Any help?

5
  • 2
    But why can't you simply use some sorting functions available in C#? Commented Jan 29, 2014 at 14:16
  • 1
    That doesn't look like Java to start with - did you mean Javascript? (That's an entirely different language.) Commented Jan 29, 2014 at 14:18
  • Did you mean Javascript? Because that doesn't look like Java (that's not even valid). Commented Jan 29, 2014 at 14:18
  • What does didn't work mean? Was there an error? How do you know it didn't work? Commented Jan 29, 2014 at 14:18
  • Ok, ok, i edited my title . Sorry. Commented Jan 29, 2014 at 14:18

1 Answer 1

4

Using LINQ, assuming Angle is a sortable property like a decimal, integer, etc:

var result = points.OrderBy(p => p.Angle);

Should your Angle be a non trivial object, or require custom sorting, you could make it implement IComparable<T> and write your own comparisson.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.