简体   繁体   English

列表中的排序算法

[英]Sorting Algorithm in a list

I'm trying to sort my list using an algorithm.我正在尝试使用算法对列表进行排序。 The list contains string arrays which holds date, titel and entry.该列表包含字符串 arrays ,其中包含日期、标题和条目。

So the error message I am getting is this:所以我得到的错误信息是这样的:

Error CS0019 Operator '>' cannot be applied to operands of type 'string[]' and 'string[]'错误 CS0019 运算符“>”不能应用于“string[]”和“string[]”类型的操作数

I will not include all of my code, as it is not in English anyway.我不会包含我的所有代码,因为它无论如何都不是英文的。

List<string[]> loggBok = new List<string[]>();

        void nyttInlägg(string datum, string titel, string text) // När användaren anropar denna metoden från menyn, så kommer hans input
                                                                 // läggas in i vektorn. 
        {
            string[] anteckingar = new string[3] { "", "", "" };
            anteckingar[0] = datum;
            anteckingar[1] = titel;
            anteckingar[2] = text;
            loggBok.Add(anteckingar);

            
            
            bool isSorted = false;
            while (!isSorted)
            {
                isSorted = true;
                for (int i = 0; i < loggBok.Count - 1; i++)
                {
                    if ((loggBok[i]) > loggBok[i + 1])
                    {
                        string[] temp = loggBok[i + 1];
                        loggBok[i + 1] = loggBok[i];
                        loggBok[i] = temp;
                    }

                    i = i + 2;
                }
            }

So basically, every 3 element in the list will hold a stringarray with a date.所以基本上,列表中的每 3 个元素都会包含一个带有日期的字符串数组。 This is the date that I want to organize.这是我要组织的日期。 I believe I understand the logic behind the algorithm but I just cannot get the syntax right.我相信我理解算法背后的逻辑,但我就是无法正确理解语法。 I have checked other threads, some with similar problems but none that was 100%.我检查了其他线程,有些有类似的问题,但没有一个是 100%。 As this is a school project it has to be an algorithm, thus I don't want to make it "easier" or more effective in anyway.由于这是一个学校项目,它必须是一个算法,因此我不想让它“更容易”或更有效。 Any ideas on how to overcome this horrible error message?关于如何克服这个可怕的错误信息的任何想法?

Some issues:一些问题:

  • if ((loggBok[i]) > loggBok[i + 1]) lacks a closing parenthesis if ((loggBok[i]) > loggBok[i + 1])缺少右括号

  • The above attempts to compare arrays , while you just want to compare the dates.以上尝试比较arrays ,而您只想比较日期。 So you need to reference the date-entry in loggBok[i] :因此,您需要参考loggBok[i]中的日期条目:

     if ((loggBok[i][0]) > loggBok[i + 1][0]))
  • The i index refers to arrays (triplets), so you should not increase it with three, but just with one, as it will go to the next triplet when you do i++ , which is what you want. i索引指的是 arrays (三元组),所以你不应该将它增加三个,而只增加一个,因为当你执行i++时,它会将 go 增加到下一个三元组,这就是你想要的。 So remove i = i + 2 , otherwise you skip some of these triplets.所以删除i = i + 2 ,否则你会跳过其中一些三元组。

The operator > greater than cannot be applied to type array or type string ( as mentioned in other answer comment) - have a look at the documentation here运算符>大于不能应用于类型数组或类型字符串(如其他答案评论中所述) - 请查看此处的文档

Each element in the list is an array of strings - with structure:列表中的每个元素都是一个字符串数组 - 具有结构:

  1. Data数据
  2. Title标题
  3. Text文本

So the following command will fails if ((loggBok[i]) > loggBok[i + 1])) as you comparing two arrays with the operator > .因此,如果((loggBok[i]) > loggBok[i + 1]))在您将两个 arrays 与运算符>进行比较时,以下命令将失败。

To retrieve the data you will have loggBok[i][0] which is a string.要检索数据,您将拥有一个字符串loggBok[i][0] You need to convert them to the correct type(eg. int) in order to use the comparison operator > .您需要将它们转换为正确的类型(例如 int)才能使用比较运算符>

I assumed the data is a type int -我假设数据是 int 类型 -

                if (int.TryParse(loggBok[i][0], out int currentElement) && 
                    int.TryParse(loggBok[i + 1][0], out int nextElement) && 
                    currentElement > nextElement)
                {
                    string[] temp = loggBok[i + 1];
                    loggBok[i + 1] = loggBok[i];
                    loggBok[i] = temp;
                }

You can read about the TryParse here您可以在此处阅读有关 TryParse 的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM