简体   繁体   中英

Sort array of floats in c#

I m new to c # . I need to sort arrays aa and bb with have all the items floats. I need to get highest and second highest in boths arrays . How can I sort the array ? I found how to sort arrays of int s , but didn t see how to sort floats . If it s not possible , what other options do I have ?

namespace program
{
    class Program
    {
        static void Main(string[] args)
        {
            float a = Convert.ToSingle(Console.ReadLine());
            float b = Convert.ToSingle(Console.ReadLine());
            float c = Convert.ToSingle(Console.ReadLine());
            float d = Convert.ToSingle(Console.ReadLine());
            float e = Convert.ToSingle(Console.ReadLine());
            float f = Convert.ToSingle(Console.ReadLine());

            float[] aa = new float[3];
            aa[0] = a;
            aa[1] = c;
            aa[2] = e;

            float[] bb = new float[3];
            bb[0] = b;
            bb[1] = d;
            bb[2] = f;




            Console.WriteLine( (aa[0]- aa[1]) * (bb[0]-bb[1]) /2 );

        }
    }
}

You can use Array.Sort(yourArray) to sort your array

once you have sorted your array then you can use the indexes to access the first and second items as per your code.

     `static void Main(string[] args)
      {
        float a = Convert.ToSingle(Console.ReadLine());
        float b = Convert.ToSingle(Console.ReadLine());
        float c = Convert.ToSingle(Console.ReadLine());
        float d = Convert.ToSingle(Console.ReadLine());
        float e = Convert.ToSingle(Console.ReadLine());
        float f = Convert.ToSingle(Console.ReadLine());

        float[] aa = new float[3];
        aa[0] = a;
        aa[1] = c;
        aa[2] = e;

        float[] bb = new float[3];
        bb[0] = b;
        bb[1] = d;
        bb[2] = f;

        Array.Sort(aa);
        Array.Sort(bb);




        Console.WriteLine( (aa[0]- aa[1]) * (bb[0]-bb[1]) /2 );

    }`

Let's start extracting ReadSingle routine:

  private static float ReadSingle(string title = null) {      
    while (true) {
      if (!string.IsNullOrWhiteSpace(title))
        Console.WriteLine(title);

      if (float.TryParse(Console.ReadLine(), out float result))
        return result;

      Console.WriteLine("Invalid format, please try again.");  
    }
  }

then create arrays:

  float[] aa = new float[] {
    ReadSingle(),  
    ReadSingle(),
    ReadSingle(), 
  };

  float[] bb = new float[] {
    ReadSingle(),  
    ReadSingle(),
    ReadSingle(), 
  };

in general case, when we have to create an array of N items we can use Linq :

  using System.Linq;

  ...

  float[] array = Enumerable
    .Range(1, N)
    .Select(i => ReadSingle($"Please, provide item #{i}"))
    .ToArray();

Finally, let's add Array.Sort and bind it all together:

namespace program {
  class Program {
    private static float ReadSingle(string title = null) {      
      while (true) {
        if (!string.IsNullOrWhiteSpace(title))
          Console.WriteLine(title);

        if (float.TryParse(Console.ReadLine(), out float result))
          return result;

        Console.WriteLine("Invalid format, please try again.");  
      }
    }

    static void Main(string[] args) {
     float[] aa = new float[] {
        ReadSingle(),  
        ReadSingle(),
        ReadSingle(), 
      };

      float[] bb = new float[] {
        ReadSingle(),  
        ReadSingle(),
        ReadSingle(), 
      };

      Array.Sort(aa);
      Array.Sort(bb); 

      Console.WriteLine((aa[0]- aa[1]) * (bb[0] - bb[1]) / 2);
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM