1

I have a jobArray array with 5 jobs that contain a description, hours needed to complete, and an hourly pay that a user enters.

I need to sort the array when it prints by ascending order of the total fee (hours * hourly pay).

The assignment requires I use the IComparable interface when doing so but I'm unsure how to use it. Any help would be appreciated, thank you

Here is my job class

class Job : IComparable
{

    public Job(string description, int hours, double hourRate, double fee)
    {
        Description = description;
        hoursToComplete = hoursToComplete;
        hourlyRate = hourlyRate;
        totalFee = totalFee;

    }

This is the interface I've done to compare totalFees

    public int CompareTo(Job o)
    {
        int returnVal;
        Job temp = (Job)o;
        if (this.totalFee > temp.totalFee)
            returnVal = 1;
        else
            if (this.totalFee < temp.totalFee)
                returnVal = -1;
            else
                returnVal = 0;
        return returnVal;

    }

I'm unsure what do to from here as to sorting the jobs when they print out by total fees.

2
  • I think you did it correct. Now just add the objects of this class to a list and use sort() method. Commented Oct 3, 2016 at 5:05
  • Your code will not compile. There are a number of obvious errors in your code that has nothing to do with IComparable or sorting. You should first attempt to fix it before trying to do anything more complex. Commented Oct 3, 2016 at 5:07

3 Answers 3

3

When you override "compareTo" method, and if you call "sort", the overrideen method is automatically called.

Here is a good example from microsoft for arrays. https://support.microsoft.com/en-us/kb/320727

Below is a an example (pseudo code) of "LIST"

List<Job> list = new List<Job>();

list.Add(new Job() { //Intialize arguments });
list.Add(new Job() { //Intialize arguments });
list.Add(new Job() { //Intialize arguments });
list.Add(new Job() { //Intialize arguments });
list.Add(new Job() { //Intialize arguments });

// Uses IComparable.CompareTo()
list.Sort();
Sign up to request clarification or add additional context in comments.

Comments

2

Here's the working version of your code:

class Job : IComparable<Job>
{
    public string Description { get; set;}

    public int HoursToComplete { get; set;}

    public double HourlyRate { get; set;}

    public double TotalFee { get; set;}

    public Job(string description, 
               int hoursToComplete, 
               double hourlyRate, 
               double totalFee)
    {
        Description = description;
        HoursToComplete = hoursToComplete;
        HourlyRate = hourlyRate;
        TotalFee = totalFee;
    }

    public int CompareTo(Job otherJob)
    {
        int returnVal;

        if (this.TotalFee > otherJob.TotalFee)
            returnVal = 1;
        else
            if (this.TotalFee < otherJob.TotalFee)
            returnVal = -1;
        else
            returnVal = 0;

        return returnVal;
    }
}

Now since you have implemented IComparable<Job>, then on the given List<Job>, just call Sort, which will automatically call Job class CompareTo, instead of object class. Also note I have used the generic version IComparable<Job>, instead of IComparable, to avoid unncessary type-casting

4 Comments

thank you. would i call it when i print out the array? when i try to call .sort it does not appear on the intellisense.
Call ArrayObject.ToList(), then call Sort on the List
You can simplify CompareTo to a one-liner. return this.TotalFee - otherJob.TotalFee
In this case true, but will not work as expected in all the cases, where multiple fields are involved, therefore used the standard comparison notation, modifying the OP code. Nonetheless thanks
2

Try CompareTo function as given below:

public int CompareTo(object obj)
{
     job tempList = (job)obj;
     return tempList.totalFee.CompareTo(totalFee);
}

Then call your method.

list.Sort()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.