Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
       }
Posted
Comments
CPallini 26-May-11 9:21am    
Simo already provided the solution. However if you wish to implement yourself the algorithm then have a look at: http://en.wikipedia.org/wiki/Sorting_algorithm

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:

C#
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;
        }
    }
}


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

Happy coding!

-MRB
 
Share this answer
 
You might want to take a look at sorting via LINQ as an alternate - see this[^].
 
Share this answer
 
Try this

MSDN : Array.sort[^]
 
Share this answer
 
Comments
ambarishtv 26-May-11 9:46am    
my 5 :)
parmar_punit 26-May-11 10:22am    
good call... my 5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900