1

I'm trying to sort my list using an algorithm. The list contains string arrays which holds date, titel and entry.

So the error message I am getting is this:

Error CS0019 Operator '>' cannot be applied to operands of type 'string[]' and '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. 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%. 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?

3
  • The most important tag is the programming language. Could you add it? Commented Jul 25, 2020 at 8:09
  • You should replace if ((loggBok[i]) > loggBok[i + 1]) with if ((loggBok[i][0]) > loggBok[i + 1][0])) (also a closing parenthesis). That will compare the dates. Commented Jul 25, 2020 at 8:12
  • It should be logbok.add() rather than Add. Also logbok.size() instead of logbok.Size(). Commented Jul 25, 2020 at 8:36

2 Answers 2

1

Some issues:

  • if ((loggBok[i]) > loggBok[i + 1]) lacks a closing parenthesis

  • The above attempts to compare arrays, while you just want to compare the dates. So you need to reference the date-entry in 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. So remove i = i + 2, otherwise you skip some of these triplets.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you mate, I added the language now which is C#. I made the changes, and the thing with reference makes sense but I am still getting the same error, or atleast now it's not saying string arrays, rather just string. CS0019 Operator '>' cannot be applied to operands of type 'string' and 'string'
0

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 >.

To retrieve the data you will have loggBok[i][0] which is a string. You need to convert them to the correct type(eg. int) in order to use the comparison operator >.

I assumed the data is a type 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

3 Comments

Worked like a charm. I was thinking it had to be solved in this way, couldn't get the syntax right tho. Thanks mate :D
@Maxkristiansson gald it works. Please consider upvoting and accepting an answer by clicking on that checkmark! Thanks
Done, but my upvote won't show as I have not voted for 15 answers yet :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.