4 Replies - 906 Views - Last Post: 25 January 2016 - 07:41 AM Rate Topic: -----

#1 Joda-Code   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-November 14

How to sort an array of a class.

Posted 24 January 2016 - 10:29 AM

Hello, I have a class "employee" that take for parameters. Here is the constructor.
public Employee(String name, int number, Decimal rate, Double hours)
        {
             SetName(name);
             SetNumber(number);
             SetRate(rate);
             SetHours(hours);

            //  Caculates gross pay and asign the value to the variable "gross"
             if (hours <= 40.0)
             {
                 gross = (decimal)hours * rate;
             }
             else
             {
                 gross = (40.0m * rate) + ((decimal)hours - 40.0m) * rate * 1.5m;
             }
             //gross = (hours <= 40.0) ? (decimal)hours * rate : (40.0m * rate) + ((decimal)hours - 40.0m) * rate * 1.5m;
        }


Now I am reading a csv file to populatate the class and create an array of employees.
static Employee[] employees = new Employee[100];    //Array of employees
        static int counter = 0;


Here I populate the array of employees.
// Open the data file
                StreamReader employeeData = new StreamReader("employees.txt");

                String lineOfData;     // Holds one line of data from sorce file

                // Reads a line of data, explode it, and add an entry to the employees array
                while ((lineOfData = employeeData.ReadLine()) != null)
                {
                    String[] explode = lineOfData.Split(',');

                    employees[counter++] = new Employee(explode[0], Convert.ToInt32(explode[1]),
                               Convert.ToDecimal(explode[2]), Convert.ToDouble(explode[3]));
                }

                // close the data  reader
                employeeData.Close();


Now I want to implement a quick sort to sort the employees array by" employee name, number, rate, hours, and gross pay". I have used the C# sort libraries and bubble sort with no problem, but I have no idea how to go about using quick sort. I am trying to learn some different sorting algorithm Thanks a lot for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: How to sort an array of a class.

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5537
  • View blog
  • Posts: 14,539
  • Joined: 18-April 07

Re: How to sort an array of a class.

Posted 24 January 2016 - 12:52 PM

Is your goal to learn quick sort or to simply sort your list in .NET? I ask because in .NET you can use IComparable and the compareTo function in your employee class to then make it sortable.

http://www.dotnetperls.com/icomparable


Nice little example of how to do that. :)

This post has been edited by Martyr2: 24 January 2016 - 12:53 PM

Was This Post Helpful? 1
  • +
  • -

#3 Joda-Code   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-November 14

Re: How to sort an array of a class.

Posted 24 January 2016 - 12:59 PM

I want to learn quick sort. I already solved it using IComparable and the compareTo function. Now I want to do it using different sort methods.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7283
  • View blog
  • Posts: 24,658
  • Joined: 05-May 12

Re: How to sort an array of a class.

Posted 25 January 2016 - 07:25 AM

Okay, show us your current implementation of your bubble sort method. The reason that I'm asking is because it'll let us determine if you understand how to write methods with parameters.
Was This Post Helpful? 0
  • +
  • -

#5 Joda-Code   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-November 14

Re: How to sort an array of a class.

Posted 25 January 2016 - 07:41 AM

I figured it out. Found a great example here. http://snipd.net/quicksort-in-c
I also wrote a selection sort.
                        swap = true; 
                        j = 0; //counter to sort the emmployee array
                        //Sort by Employee Name using selection sort.
                        while (swap)
                        {
                            swap = false; 
                            j++;
                            for (int i = 0; i < employees.Length - j; i++)
                            {
                                if (employees[i] != null && employees[i + 1] != null)
                                {
                                    if (employees[i].GetName().CompareTo(employees[i + 1].GetName()) > 0)
                                    {
                                        tmp = employees[i];
                                        employees[i] = employees[i + 1];
                                        employees[i + 1] = tmp;
                                        swap = true;
                                    }
                                }

                            }
                        }


Thanks a lot
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1