2017-07-23 28 views
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列表以排序时遇到问题。如果有人对我有一个想法,或者有更好的方法来解决这个问题,我就会全神贯注。谢谢!如果您需要更多的澄清,请随时提出更多问题。

回答

1

您可以在每个FOREACH之前的sort()之下。

Helper类更改public,所以它是从void Main()访问,然后下面写你的Comparer

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; 
    } 
} 

每个FOREACH之前,你叫sort()特别Comparer这样的:

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

     } 

您可以为其余的比较器做到这一点。

+0

这使得有很大的意义,但我仍然得到关于 aZoo.Sort(sortByPurchaseCost)的错误它说,我无法从“A2_Zoo_Sorts_BahlerArslan,Class1.sortPurchaseCostAscendingHelper‘到’System.Collections.Generic.IComparer “any thoughts? – Arsy

+0

你可以告诉我你的完整代码吗?你的'sortPurchaseCostAscendingHelper'应该继承'IComparer ',而不仅仅是'IComparer' – zzT

+0

好吧,我把我的代码更新到了我目前的版本。问题,但由于某种原因它仍然没有正确排序谢谢你的帮助@zzT – Arsy