2

I am having an issue sorting some objects with certain criteria. I have searched but it seems like no one is doing it the way I am or maybe my situation is unique or I am doing it all wrong. I am trying to sort a few objects within visual studio via C#. I have several animal classes and a zoo class (interface) as well as a sorting class. In my main class I have the following where I make a list and try to sort the list of animals I made:

class Program
{
    static void Main(string[] args)
    {

            Lion aLion = new Lion("Leo", DateTime.Parse("4/04/2012"),
                2500, 360, 0, 9);
            Giraffe aGiraffe = new Giraffe("Amber", DateTime.Parse("3/23/2013"),
                4500, 1400, 0, 25);
            Giraffe aSecondGiraffe = new Giraffe("Charlie", DateTime.Parse("5/2/2012"),
                3600, 2600, 0, 25);
            Giraffe aThirdGiraffe = new Giraffe("George", DateTime.Parse("5/22/2013"),
                3000, 3200, 0, 3);
            Lion aThirdLion = new Lion("Charlie", DateTime.Parse("1/10/2011"),
                1000, 6, 0, 27);
            Lion aSecondLion = new Lion("Bernie", DateTime.Parse("1/30/2012"),
                1200, 8, 0, 27);
            Lion aFourthLion = new Lion("Billy", DateTime.Parse("12/12/2012"),
                5000, 350, 0, 20);
            Lion aFifthLion = new Lion("Jake", DateTime.Parse("10/15/2015"),
               10000, 400, 0, 20);
            Giraffe aFifthGiraffe = new Giraffe("Mike", DateTime.Parse("5/2/2016"),
                8000, 620, 0, 10);
            Giraffe aFourthGiraffe = new Giraffe("Joe", DateTime.Parse("5/17/2014"),
                8500, 645, 0, 10);


        List<IZoo> aZoo = new System.Collections.Generic.List<IZoo>();

        aZoo.Add(aLion);
        aZoo.Add(aGiraffe);
        aZoo.Add(aSecondGiraffe);
        aZoo.Add(aThirdGiraffe);
        aZoo.Add(aThirdLion);
        aZoo.Add(aSecondLion);
        aZoo.Add(aFourthLion);
        aZoo.Add(aFifthLion);
        aZoo.Add(aFifthGiraffe);
        aZoo.Add(aFourthGiraffe);


        string fileSpec = "LogData.txt";
        StreamWriter output = File.CreateText(fileSpec);

        var sortByPurchaseCost = new Class1.sortPurchaseCostAscendingHelper();
        aZoo.Sort(sortByPurchaseCost);
        Console.WriteLine(PrintReportHeader());
        foreach (IZoo animalData in aZoo)
        {
            Console.WriteLine($"{animalData}");

        }
        Console.WriteLine(" ");

        var sortBysortWeightAndDOB = new Class1.sortWeightAndDOBHelper();
        aZoo.Sort(sortBysortWeightAndDOB);
        Console.WriteLine(PrintReportHeader());
        foreach (IZoo animalData in aZoo)
        {
            Console.WriteLine($"{animalData}");

        }

        Console.WriteLine(" ");

        var sortCagePurchaseCostAnimalNameID = new Class1.sortCagePurchaseCostAnimalNameIDHelper();
        aZoo.Sort(sortCagePurchaseCostAnimalNameID);
        Console.WriteLine(PrintReportHeader());
        foreach (IZoo animalData in aZoo)
        {
            Console.WriteLine($"{animalData}");

        }

        Console.WriteLine(" ");

        var sortAgeWeight = new Class1.sortAgeWeightHelper();
        aZoo.Sort(sortAgeWeight);
        Console.WriteLine(PrintReportHeader());
        foreach (IZoo animalData in aZoo)
        {
            Console.WriteLine($"{animalData}");

        }

        Console.WriteLine(" ");

        var sortTypePCCageName = new Class1.sortTypePCCageNameHelper();
        aZoo.Sort(sortTypePCCageName);
        Console.WriteLine(PrintReportHeader());
        foreach (IZoo animalData in aZoo)
        {
            Console.WriteLine($"{animalData}");

        }
        output.Close();

        Console.WriteLine("\nPress <ENTER> to quit...");
        Console.ReadKey();
    }

    public static string PrintReportHeader()
    {
        return $"{"ID",-7} {"Animal Type",-15} {"Name",-15} {"Weight",-8}" + 
            $"{"Age",-5} {"Purchase Cost",-16} {"Cage No.",-10}\n" + 
            $"{"==",-7} {"===========",-15} {"====",-15} {"======",-8}" + 
            $"{"===",-5} {"=============",-16} {"========",-10}"; }

In my sorting class I have the following:

public class sortPurchaseCostAscendingHelper : IComparer<IZoo>
    {
        public int Compare(IZoo z1, IZoo z2)
        {


            if (z1.PurchaseCost > z2.PurchaseCost)
                return 1;

            if (z1.PurchaseCost < z2.PurchaseCost)
                return -1;

            else
                return 0;
        }
    }

    public class sortWeightAndDOBHelper : IComparer<IZoo>
    {
        public int Compare(IZoo z1, IZoo z2)
        {

            if (z1.Weight > z2.Weight && z1.DOB > z2.DOB)
                return 1;

            if (z1.Weight < z2.Weight && z1.DOB < z2.DOB)
                return -1;

            else
                return 0;
        }

        public static implicit operator sortWeightAndDOBHelper(sortAgeWeightHelper v)
        {
            throw new NotImplementedException();
        }
    }

    public class sortCagePurchaseCostAnimalNameIDHelper : IComparer<IZoo>
    {
        public int Compare(IZoo z1, IZoo z2)
        {

            if (z1.CageNumber > z2.CageNumber && z1.PurchaseCost > z2.PurchaseCost && z1.Name > z2.Name && z1.ID > z2.ID)
                return 1;

            if (z1.Weight < z2.Weight && z1.DOB < z2.DOB)
                return -1;

            else
                return 0;
        }
    }

    public class sortAgeWeightHelper : IComparer<IZoo>
    {
        public int Compare(IZoo z1, IZoo z2)
        { 

            if (z1.DOB > z2.DOB && z1.Weight > z2.Weight)
                return 1;

            if (z1.DOB < z2.DOB && z1.Weight < z2.Weight)
                return -1;

            else
                return 0;
        }
    }

    public class sortTypePCCageNameHelper : IComparer<IZoo>
    {
        public int Compare(IZoo z1, IZoo z2)
        {

            if (z1.PurchaseCost > z2.PurchaseCost && z1.CageNumber > z2.CageNumber)
                return 1;

            if (z1.PurchaseCost < z2.PurchaseCost && z1.CageNumber < z2.CageNumber)
                return -1;

            else
                return 0;
        }
    }
}

Each Icomparer needs to sort the five FOREACH sections differently. I am having trouble trying to link these Icomparers to my main FOREACH list in order to sort. If anyone has an idea for me or maybe a better way at going about this I am all ears. Thanks! Please feel free to ask more questions if you need anymore clarification.

1 Answer 1

1

You can sort() before each FOREACH like below.

Change your Helper class to public, so it's accessible from void Main(), then write up your Comparer as below:

public class sortPurchaseCostAscendingHelper : IComparer<IZoo>
{
    public int Compare(IZoo z1, IZoo z2)
    {
        if (z1.PurchaseCost > z2.PurchaseCost)
            return 1;

        if (z1.PurchaseCost < z2.PurchaseCost)
            return -1;

        else
            return 0;
    }
}

Before each FOREACH, you call sort() with particular Comparer like this:

var sortByPurchaseCost = new sortPurchaseCostAscendingHelper();
        aZoo.Sort(sortByPurchaseCost);
        foreach (IZoo animalData in aZoo)
        {
            Console.WriteLine($"{animalData}");

        }

You can do it for the rest of your comparers.

Sign up to request clarification or add additional context in comments.

5 Comments

This makes a lot of sense but I am still getting an error on aZoo.Sort(sortByPurchaseCost) it says that I cannot convert from 'A2_Zoo_Sorts_BahlerArslan,Class1.sortPurchaseCostAscendingHelper" to "System.Collections.Generic.IComparer<A2_ZooSorts_BahlerArslan.IZoo>" any thoughts?
Can you show me your full code of it? Your sortPurchaseCostAscendingHelper should inherit IComparer<IZoo>, not just IComparer
Okay I updated my code above to what I currently have. I fixed the original problem but for some reason It is still not sorting properly. Thank you for your help @zzT
It worked! I am a heavy Java programmer and I always forget to rebuild and clean my project when using Visual Studio. Rookie mistakes! Thanks again @zzT
OK, dear. Seems like someone also interested in your problem, you can mark my answer as final answer, or update your code in your question, so it helps our community. Thanks.

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.