2

I need in C# to sort some distances in some objects which are in float. I use delegate and Array.Sort to sort them but it seems that I can't use float.

How can I keep the precision of those float when converting to int?

Is it possible to use Array.Sort with float instead of int in return?

Without using LINQ.

7
  • You can't convert float to int without losin precision. show as some sample of your code. Commented Aug 17, 2012 at 12:14
  • possible duplicate of How do I order an array of floating point numbers using a criteria other than size, using LINQ? Commented Aug 17, 2012 at 12:15
  • Btw, although the title of that question mentions LINQ there are a number of correct answers that do use Comparer or Comparison instead of LINQ that should work just as well for you. Commented Aug 17, 2012 at 12:17
  • Why don't you want to use LINQ? Commented Aug 17, 2012 at 12:19
  • I saw a lot of posts with LINQbut I can't use LINQ. Commented Aug 17, 2012 at 12:19

6 Answers 6

8

Array.Sort<T>(T[] array, Comparison<T> comparison) is not restricted to any type as long as you provide correct comparison delegate. Comparison delegate should return integer value containing the result of compare operation, not a value to be compared with something. For example, if you have class X with float property Property, you can sort them like this:

public sealed class X
{
    public float Property { get; set; }
}

static void Test()
{
    var arr = new X[]
    {
        new X { Property = 5.5f },
        new X { Property = 2.5f },
        new X { Property = 6.5f },
        new X { Property = 1.2f },
    };
    Array.Sort(arr, (a, b) => a.Property.CompareTo(b.Property));
}
Sign up to request clarification or add additional context in comments.

Comments

6

You can use LINQ OrderBy extension method

var arr = new[] { 1.3f, 1.4f };
arr.OrderBy( a => a ).ToArray();

1 Comment

he added it later. Ok, I'm sorry, no limitations was given at first
2

You could use LINQ?

arrayOfFloats = arrayOfFloats.OrderBy(x => x);

Comments

2

The best variant is to use radixsort, see more at

Is there a good radixsort-implementation for floats in C#

Good luck

Comments

1

I guess you are using some older .Net version since in one of your comments you have asked that you do not want to use LINQ.

In that case Yes it is possible use Array.Sort

float[] myfloatarray = new float[5];

Array.Sort(myfloatarray);

This will use

Array.Sort<T>(T[] array);

This does not use any LINQ only generics and is available in .Net 2.0.

Read about Array.Sort here

Comments

1

Maybe you're confusing the "in" type T which is allowed to be anything, with the return type of the Comparison<T> delegate which must be int?

This works fine:

float[] arr = { 2.3f, 1.1f, 9.0f, 6.6f, };
Array.Sort(arr, (x, y) => SomeMethodReturningInt(x, y));

The second parameter to Sort is a Comparison<float> in this case. In the lambda, x and y are floats.

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.