【问题标题】:How can i merge 2 lists to 1 so i can sort it together in c#?如何将 2 个列表合并为 1 个列表,以便在 c# 中将其排序在一起?
【发布时间】:2019-09-06 09:10:27
【问题描述】:

我想做一个应用程序,我可以在其中输入一个单词,然后输入一个数字。在输入我想要的所有内容后,我希望它们的输出按字母顺序排序。问题是单词排序但数字不与它们一起排序,因为它们是 2 个不同的列表。我如何合并这两个列表?我不想像 AddRange 那样将它们加在一起我需要像 Console.WriteLine ("x{0}+" "+"{1}", numbers, words 之类的输出。

我试过 words.Sort();但它只是对单词进行排序,而不是同时排序。那么如何合并这两个列表呢?

问题可能很清楚,但如果您需要一些代码,那就是:

   using System;
   using System.Collections;
   using System.Collections.Generic;
   using System.Linq;
   using System.Threading;

  namespace zutaten
  {
      namespace mengen
      {
          class Program
          {
          static int hello;
          static List<string> zutaten = new List<string>();
          static List<int> mengen = new List<int>();
          public static int memo;
          public static int d = 1;
          static List<string> zusammen = new List<string>();
          static public bool Main(string[] args)
          {

              bool fertig = false;

            while (!fertig)
            {
                var wert = Console.ReadLine();
                if (wert != "f")
                {
                    if (Gleich(zutaten, mengen))//zutaten.Count == mengen.Count)
                    {
                        if (KeineZutaten(wert))//int.TryParse(wert, out int keineZutat))
                        {
                            KeineZutatenAussage(wert);
                        }
                        else
                        {
                            if (Beinhaltet(wert)) //zutaten.Contains(wert)
                            {
                                Removen(wert);
                                //    int index = zutaten.IndexOf(wert);
                                //    zutaten.RemoveAt(index);
                                //    mengen.RemoveAt(index);
                            }
                            //-------
                            Zutathinzufügen(wert);
                            //zutaten.Add(wert);
                        }
                    }
                    else
                    {
                        if (ParseMenge(wert, out memo))//int.TryParse(wert, out int menge))
                        {
                            Mengehinzufügen(memo);// mengen.Add(menge);
                        }
                        else
                        {
                            Mengepluseins(mengen);
                            //mengen.Add(1);

                            //--------

                            if (Beinhaltet(wert))
                            {
                                Removen(wert);
                                //    int index = zutaten.IndexOf(wert);
                                //    zutaten.RemoveAt(index);
                                //    mengen.RemoveAt(index);

                            }
                            //------
                            Zutathinzufügen(wert);
                            //zutaten.Add(wert);

                        }
                    }
                }
                else
                {
                    fertig = Fertigt();

                    if (!Gleich(zutaten, mengen))
                    {
                        Mengepluseins(mengen);
                    }

                    Forschleife(zutaten);

                    //for (int i = 0; i < zutaten.Count; i++)
                    //{
                    //    Console.WriteLine("x{0} {1}", mengen[i], zutaten[i]);
                    //}
                }
            }
        }





        public static string MeineMethode()
        {
            return "string";
        }

        public static bool Gleich(List<string> variable1, List<int> variable2)
        {
            return variable1.Count == variable2.Count;
        }

        public static bool KeineZutaten(string wert1)
        {
            return int.TryParse(wert1, out hello);
        }

        public static void KeineZutatenAussage(string wert2)
        {
            Console.WriteLine("{0} ist keine Zutat", wert2);
        }

        public static bool Beinhaltet(string hulu)
        {
            return zutaten.Contains(hulu);
        }

        public static void Removen(string wertt)
        {
            int index = zutaten.IndexOf(wertt);
            zutaten.RemoveAt(index);
            mengen.RemoveAt(index);
        }

        public static void Zutathinzufügen(string werttt)
        {
            zutaten.Add(werttt);
        }
        //    int index = zutaten.IndexOf(wert);
        //    zutaten.RemoveAt(index);
        //    mengen.RemoveAt(index);

        //int.TryParse(wert, out int keineZutat
        //zutaten.Add(wert);

        public static bool ParseMenge(string wert1, out int var2)
        {
            return int.TryParse(wert1, out var2);
        }
        //int.TryParse(wert, out int menge))

        public static void Mengehinzufügen(int var1)
        {
            mengen.Add(var1);
        }
        // mengen.Add(menge);

        public static void Mengepluseins(List<int> mengen)
        {
            mengen.Add(d);
        }
        //mengen.Add(1);

        public static bool Fertigt()
        {
            return true;
        }
        //fertig = true;

        public static bool Mengeungleichzutaten(List<string> variable1, List<int> variable2)
        {
            return variable1.Count != variable2.Count;
        }                                                                                                      
        //if (mengen.Count != zutaten.Count)                                                                   
        //{                                                                                                    
        //    mengen.Add(1);                                                                                                                    
        //}

        public static void Forschleife(List<string> hey)
        {

            zutaten.Sort();
            for (int i = 0; i < hey.Count; i++)
            {
                Console.WriteLine("x{0}"+" "+"{1}, mengen[i], zutaten[i]);
            }
}

输入示例:

Pork[Enter]
3[Enter] 
Tomatoes[Enter]
6[Enter]
Potatoes[Enter]
2[Enter] 

预期输出:

3x Pork 
2x Potatoes 
6x Tomatoes 

当前输出:

3x Pork 
6x Potatoes 
2x Tomatoes

【问题讨论】:

  • 而不是List&lt;string&gt;List&lt;int&gt; 使用List&lt;MyClassStringNumberPair&gt;(或List&lt;Tuple&lt;string,int&gt;&gt;
  • 如果您使用数组而不是列表,您可以使用Array.Sort(Array, Array) 同时对两个数组进行排序。
  • @MatthewWatson 我认为你是对的。您可以将此作为答案发布,我删除了我的答案
  • @Cid 不过,他可能无法使用数组 - 我会等待 OP 回复。另外,我认为 u/Selvin 的想法最好。
  • @Cid 在代码末尾完成。

标签: c# list sorting


【解决方案1】:

假设这些可以是数组,那么你可以使用原生的排序命令,根据其中一个对两个数组进行排序。此示例按分数排序:

        static void Main(string[] args)
        {
            int[] classScores = new int[]{30,50,25,39,62};
            string[] studentNames = new string[]{"Jim","John","Mary","Peter","Sarah"};
                Array.Sort(classScores, studentNames);   // sort both according to scores
            for (int i = 0; i < classScores.Length; i++)
            {
                Console.WriteLine(classScores[i] + " " + studentNames[i]);
            }
        }

【讨论】:

    【解决方案2】:

    您可以使用SortedDictionary 来存储您的值。键是产品名称,值是数量。这将自动按键排序。

    static SortedDictionary<string, int> GetProducts()
    {
        //                               Type of the
        //                                   key     
        //                                    |  Type of the
        //                                    |    value
        //                                    |      |
        //                                    v      v
        var Products = new SortedDictionary<string, int>();
    
        while (true)
        {
            var product = "";
            int quantity;
            // First, ask the user to enter a product. If he enters nothing, ask again
            while (product == "")
            {
                Console.WriteLine("Please enter a product name or f to finish");
                product = Console.ReadLine();
                if (product == "f")
                    return Products; // we are done, we can return the SortedDictionary
            }
    
            // Now, get the quantity
            do
            {
                Console.WriteLine($"Please enter a quantity for {product}");
              // Ask again if the user enters an invalid number
            } while (!Int32.TryParse(Console.ReadLine(), out quantity));
            // Store the informations in the SortedDictionary
            Products[product] = quantity;
        }
    }
    
    public static void Main()
    {
        // Get the products
        var Products = GetProducts();
        // Display them
        foreach (var key in Products.Keys)
            Console.WriteLine($"{Products[key]}x {key}");
    }
    

    输入:

    Please enter a product name or f to finish
    Pork
    Please enter a quantity for Pork
    3
    Please enter a product name or f to finish
    Tomatoes
    Please enter a quantity for Tomatoes
    6
    Please enter a product name or f to finish
    Potatoes
    Please enter a quantity for Potatoes
    2
    Please enter a product name or f to finish
    f
    

    输出:

    3x Pork
    2x Potatoes
    6x Tomatoes
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2016-04-18
      • 1970-01-01
      • 2013-10-18
      • 2016-03-13
      • 2011-05-17
      • 1970-01-01
      相关资源
      最近更新 更多