Search code examples
c#arraysobjectbubble-sort

C#, Creating an Array in an Object Class and executing the method, custom sorting method


C#, I'm trying to create an array in a class as an object. I want this program to run the method when I do a custom sort. I need to understand how to pass the value from the decimal array, already created from text to this object? I got something wrong somewhere. All I'm getting is the name of the form when I print out the array on the other side of front end of the form.

main form using call to class: Sort sort = new Sort(rawArray);

using System;


namespace BbblSrtProj
{
    
    public class Sort
    {

        private decimal[] theArray;
        public Sort() { }
        public Sort (decimal[] sort)
        {
            this.theArray = sort;
            
        }
        public decimal[] TheArray
        {
            get
            {
                return theArray;
            }
            set 
            {
                theArray = value;
            }
        }

        //Sort Method: Bubble Sort
        public Array SortingMethod()
        {
            for (int i = 0; i <= TheArray.Length - 1; i++)
            {
                // Temp int variable to hold value in
                decimal temp;

                // Swap out adjacent value by order,
                // till completed.
                for (int j = 0; j < TheArray.Length - 1; j++)
                {
                    if (TheArray[j] > TheArray[j + 1])
                    {
                        temp = TheArray[j + 1];
                        TheArray[j + 1] = TheArray[j];
                        TheArray[j] = temp;
                    }
                }
            }

            return TheArray;
        
        }
    }
}

Solution

  • // Sort.cs 
    //This Got it working
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
        namespace SortItProj
        {
        
            public class Sort
            {
                private decimal[] theArray;
        
                public Sort(decimal[] sort)
                {
                    this.theArray = sort;
                }
        
                public decimal[] TheArray
                {
                    get
                    {
                        return theArray;
                    }
                    set
                    {
                        theArray = value;
                    }
            }
    
            public Array SortingMethod()
            {
                for (int i = 0; i <= TheArray.Length - 1; i++)
                {
                    // Temp int variable to hold value in
                    decimal temp;
    
                    // Swap out adjacent value by order,
                    // till completed.
                    for (int j = 0; j < TheArray.Length - 1; j++)
                    {
                        if (TheArray[j] > TheArray[j + 1])
                        {
                            temp = TheArray[j + 1];
                            TheArray[j + 1] = TheArray[j];
                            TheArray[j] = temp;
                        }
                    }
                }    
                return TheArray;    
            }
        }
    
    }
    
    // SortItProj.cs
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    namespace SortItProj
    {
        internal class Program
        {
            static void Main()
            {
                Random random = new Random();
                decimal[] theArray = new decimal[55];
    
                int theArrayLength = theArray.Length;   
                decimal[] theArray2 = new decimal[theArrayLength];
                
                int i = 0;
    
                do
                {
                    theArray[i] = (random.Next(1, 1000));
                    i++;
    
                } while (i < theArray.Length);
    
                Array.Copy(theArray, theArray2, theArrayLength);
    
                Sort sort = new Sort(theArray);
    
                sort.SortingMethod();
    
                int j = 0;
                foreach (decimal number in theArray)
                {
                    Console.Write(@$"{theArray2[j]} < RAW | SORTED > ");
                    Console.WriteLine(number + "\n");
                    j++;
                }
            }
        }
    }