LINQ OrderBy不按字符串的预期排序

时间:2015-10-27 19:59:39

标签: c# linq razor

我正在使用Umbraco CMS尝试对节点的集合(DynamicNode类型)进行排序,并且LINQ OrderBy语句无法正常工作。我必须在这里遗漏一些东西吗?

我一直在调试它..

DynamicNodeList thingsToDo = Model.Descendants("Tourism-Things-To-Do");
 if (thingsToDo != null && thingsToDo.Count() > 0)
    {
       foreach(dynamic thingToDo in thingsToDo)
        {
           @thingToDo.Name <br />   
        }
        <br />

        foreach(dynamic thingToDo in thingsToDo.OrderBy(x => x.Name, StringComparer.CurrentCultureIgnoreCase))
        {
           @thingToDo.Name <br />   
        }
    }

输出:

[Unsorted List from First Iteration]
Quality Inn & Suites 
​Days Inn 
Palm Tree Inn 
​Super 8 
​Wyndie Crest Bed & Breakfast

[Should be Alphabetical List from Second Iteration]
​Days Inn 
​Super 8 
​Wyndie Crest Bed & Breakfast 
Palm Tree Inn 
Quality Inn & Suites 

有人可以借给我一些见解,说明为什么这不起作用?此代码中的Name只是一个字符串。

2 个答案:

答案 0 :(得分:1)

你的字符串可能是从不可打印的字符开始的。我建议你渲染原始字符串并查看它:

afterLoad: function() {
      _gaq.push(['_trackEvent','Lightbox','Open',this.href, ,true]);
 },

答案 1 :(得分:1)

我能够将字符串分成一个char数组,我发现问题是在我的一些字符串的开头是ZERO-WIDTH空格字符的实例。我擦洗了这些字符的数组,并将它们添加回列表,现在一切正常。

<强> CODE:

SortedDictionary<string, int> sd = new SortedDictionary<string, int>();
        foreach(DynamicNode thingToDo in thingsToDo)
        {
            List<char> temp = new List<char>();
            char[] parseWord = thingToDo.Name.ToCharArray();
            var brokenWord = parseWord.Select(x => x == 8203);

            var enumeratorWord = parseWord.GetEnumerator();
            var enumeratorBool = brokenWord.GetEnumerator();
            enumeratorBool.MoveNext();
             while(enumeratorWord.MoveNext())
             {
                 if(!(bool)enumeratorBool.Current)
                 {
                     temp.Add((char)enumeratorWord.Current);
                 }
             }
             var result = string.Join("", temp.ToArray());
             sd.Add(result, thingToDo.Id);   
        }