2
我有一個問題,按照某些條件排序某些對象。我已經搜索過,但似乎沒有人按照我的方式進行,或者我的情況是獨特的,或者我做的都是錯的。我想通過C#在Visual Studio中對幾個對象進行排序。我有幾個動物類和一個動物園類(界面)以及一個排序類。在我的主類我有以下的,我做一個列表,並試圖解決動物的名單我做:C#中有多個條件和多個變量的排序列表
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}"; }
在我的排序類我有以下幾點:
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;
}
}
}
每個IComparer的需求以不同的方式對五個FOREACH部分進行排序。我在嘗試將這些Icomparers鏈接到我的主FOREACH列表以排序時遇到問題。如果有人對我有一個想法,或者有更好的方法來解決這個問題,我就會全神貫注。謝謝!如果您需要更多的澄清,請隨時提出更多問題。
這使得有很大的意義,但我仍然得到關於 aZoo.Sort(sortByPurchaseCost)的錯誤它說,我無法從「A2_Zoo_Sorts_BahlerArslan,Class1.sortPurchaseCostAscendingHelper‘到’System.Collections.Generic.IComparer「any thoughts? –
Arsy
你可以告訴我你的完整代碼嗎?你的'sortPurchaseCostAscendingHelper'應該繼承'IComparer',而不僅僅是'IComparer' –
zzT
好吧,我把我的代碼更新到了我目前的版本。問題,但由於某種原因它仍然沒有正確排序謝謝你的幫助@zzT – Arsy