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

标签 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 部分进行不同的排序。我在尝试将这些 Icomparer 链接到我的主要 FOREACH 列表以便进行排序时遇到问题。如果有人对我有想法或者可能有更好的方法来解决这个问题,我会洗耳恭听。谢谢!如果您需要更多说明,请随时提出更多问题。

最佳答案

您可以在每个 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 之前,您使用特定的 Comparer 调用 sort(),如下所示:

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

        }

您可以对其余的比较器执行此操作。

关于c# - 在 C# 中使用多个条件和多个变量对列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45261001/

相关文章:

c# - 实时合并 3D 对象

c# - 当 ListView 的 ItemsSource 改变时触发事件

algorithm - 在 QuickSort 中选择一个枢轴

c - 基数排序时如何使字符串粘在一起?

.net - 反转 ListBox 项目(按 "nothing"降序排序)

c# - 在 map 内创建障碍

c# - 使用 HtmlAgilityPack 获取 html 标签的值

javascript - Tablesorter 2.0 插件和带有嵌套 HTML 的表格

c# - 异步调用是需要在当前进程中多出一个线程还是使用线程池中的另一个线程?

javascript - 在排序 html 元素时使用 jQuery appendTo()