Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在链表中插入字符串并对其排序_C#_Linked List_Nodes - Fatal编程技术网

C# 在链表中插入字符串并对其排序

C# 在链表中插入字符串并对其排序,c#,linked-list,nodes,C#,Linked List,Nodes,我试图在链表中插入一个字符串节点并对其排序。我遇到的问题是行while((curr!=null)&&(str>curr.Data))。我得到错误“运算符或

我试图在链表中插入一个字符串节点并对其排序。我遇到的问题是行
while((curr!=null)&&(str>curr.Data))
。我得到错误“运算符<不能应用于'string'和'string'类型的操作数”。我不理解这个错误,因为该函数可以在==下正常工作,并且在它们是int而不是字符串的情况下也可以工作。我尝试过使用
str.CompareTo(currData)
,但这会产生类似的错误。如果有人能向我解释为什么会产生这个错误,以及我能做些什么来避免它,那就太好了

private Node head = null; // initially nothing in the list
    private int count = 0;

    public void InsertSorted(string str)
    {
        Node newNode = new Node(str);
        Node previous = null;
        Node curr = head;

        while((curr != null) && (str > curr.Data))
        {
            previous = curr;
            curr = curr.Next;
        }
        if(previous == null)
        {
            newNode.Next = curr;
            head = newNode;
        }
        else
        {
            newNode.Next = curr;
            previous.Next = newNode;
        }

    }
试一试


运算符对C#中的字符串无效

啊,我不知道是这样的。多谢各位,;这解决了我的错误。我不必经常进行字符串比较。我可能会自己先尝试>或<。
while((curr != null) && (string.Compare(str, curr.Data) > 0))