在C#中使用多个条件和多个变量对列表进行排序

时间:2017-07-23 02:31:00

标签: c# sorting

我遇到了使用某些条件对某些对象进行排序的问题。我已经搜索了但似乎没有人像我一样这样做,或者我的情况可能是独一无二的,或者我做错了。我试图通过C#在visual studio中对一些对象进行排序。我有几个动物类和一个动物园类(接口)以及一个排序类。在我的主要课程中,我有以下内容,我列出了一个列表并尝试对我制作的动物列表进行排序:

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 个答案:

答案 0 :(得分:1)

您可以在sort()之前FOREACH,如下所示。

将您的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}");

        }

你可以为其余的比较者做到这一点。