对数组中的数字进行排序 [英] sort numbers in array

查看:87
本文介绍了对数组中的数字进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我的问题是从最小到最大或其他方式对数字进行排序.
谁能帮助我.
谢谢

我做到了

Hi!
my problem is to sort numbers from least to most or other way around.
can anyone help me plz.
Thank you

I did this

static void FunctionPrintSortedNumbers(int[] matrix)
       {
           int least = matrix[0];
           for (int i = 0; i < matrix.Length; i++)
           {
               if (matrix[i] < least)
                   least = matrix[i];
           }
           Console.WriteLine(least);

           int[] field_without_least = ElementToThrowFromField(matrix, least);
           FunctionPrintSortedNumbers(field_without_least);
       }
       static int[] ElementToThrowFromField(int[] matrix1, int throwElement)
       {
           FunctionPrintSortedNumbers(matrix1);
           foreach (int least1 in matrix1)
           if(least1 == throwElement)
           //I tryed to do this in function but i dont know will this work
           //or how to continue
       }

推荐答案

尝试一下

MSDN:Array.sort [
Try this

MSDN : Array.sort[^]


您可能会想看一下通过LINQ进行排序的方法-请参见 [
You might want to take a look at sorting via LINQ as an alternate - see this[^].


关于的部分" ...从最小到最大..."让我觉得您正在寻找一种计算数字频率并对其进行排序的方法.无论如何,我准备了一个通用的解决方案,该解决方案既可以进行排序又可以将它们转储到控制台:

The part about "... from least to most ..." made me think you were after a method to count the frequencies of the numbers and sort after those. Anyway I cooked up a generic solution that will do both sorts and dump them to the console:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortNumbers
{

    class Program
    {
        static void Main(string[] args)
        {

            Sorter<int>.RunTest(new int[] {
                                              10, 3, 4, 10, 5, 3, 4, 10, 1, 2, 3, 5, 10, 8, 9, 9, 2, 4
                                          });
            Sorter<String>.RunTest(new String[] {
                                                     "This", "is", "a", "is", "test", "This", "a", "a", "a", "is", "test"
                                                });
            Console.ReadLine();

        }
    }

    class Sorter<T> where T : IComparable
    {
        public T[] Matrix;

        public static void RunTest(T[] matrix)
        {
            Sorter<T> test = new Sorter<T>();
            test.Matrix = matrix;
            T[] plain = test.SortPlain();
            FrequencyNumber<T>[] frequ = test.SortByFrequency();
            Sorter<T>.Dump(plain);
            Sorter<T>.DumpFrequencies(frequ);
        }

        public T[] SortPlain()
        {
            T[] sorted = new T[Matrix.Length];
            Array.Copy(Matrix, sorted, Matrix.Length);
            Array.Sort(sorted);
            return sorted;
        }

        public FrequencyNumber<T>[] SortByFrequency()
        {
            List<FrequencyNumber<T>> sorted = new List<FrequencyNumber<T>>();
            foreach(T num in Matrix)
            {
                FrequencyNumber<T> toAdd = new FrequencyNumber<T>(num);
                FrequencyNumber<T> inList = sorted.Find(delegate(FrequencyNumber<T> candidate)
                                               {
                                                   return candidate.Equals(toAdd);
                                               });
                if (inList != null)
                    inList.Frequency++;
                else
                    sorted.Add(toAdd);
            }
            sorted.Sort();
            return sorted.ToArray<FrequencyNumber<T>>();
        }

        public static void Dump(T[] arr)
        {
            Console.WriteLine("\nPlain sorted!");
            Console.WriteLine("-------------");
            int idx = 0;
            foreach (T elem in arr)
            {
                idx++;
                Console.WriteLine("{0:###}. {1:########}", idx, elem);
            }
        }

        public static void DumpFrequencies(FrequencyNumber<T>[] arr)
        {
            Console.WriteLine("\nFrequency sorted!");
            Console.WriteLine("-------------");
            int idx = 0;
            foreach (FrequencyNumber<T> num in arr)
            {
                idx++;
                Console.WriteLine(String.Format("{0:###}. Freq: {1:####} Number: {2:#######}", idx, num.Frequency, num.Number));
            }
        }
    }

    class FrequencyNumber<N> : IComparable where N : IComparable
    {
        public long Frequency;
        public N Number;
        public FrequencyNumber(N number)
        {
            Frequency = 1;
            Number = number;
        }

        public int CompareTo(object obj)
        {
            if (obj != null && !(obj is FrequencyNumber<N>))
            {
                throw new ArgumentException(String.Format("Can't compare a {0} with a {1}", this.GetType(), obj.GetType()));
            }
            FrequencyNumber<N> num = (FrequencyNumber<N>)obj;
            return Frequency.CompareTo(num.Frequency);
        }

        public bool Equals(object num)
        {
            if(num is FrequencyNumber<N>)
                return this.Number.Equals(((FrequencyNumber<N>)num).Number);
            else
                return false;
        }
    }
}



如果您有任何疑问,请给我留言!
在Visual Studio 2010,.NET 4.0中进行了测试.

祝您编程愉快!

-MRB



If you have questions leave me a comment!
Tested in Visual Studio 2010, .NET 4.0.

Happy coding!

-MRB


这篇关于对数组中的数字进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆