Page 1 of 1

# 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;    //Array of employees
static int counter = 0;
```

Here I populate the array of employees.
```// Open the data file

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, Convert.ToInt32(explode),
Convert.ToDecimal(explode), Convert.ToDouble(explode));
}

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 Reputation: 5537
• 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

## 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.

### #4 Skydiver Reputation: 7283
• 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.

## 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

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }