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.