Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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语言中按顺序排列的文本相关性列表#_C#_.net_String - Fatal编程技术网

C# C语言中按顺序排列的文本相关性列表#

C# C语言中按顺序排列的文本相关性列表#,c#,.net,string,C#,.net,String,我有一份如下机构名单: List<Institute> instituteList = new List<Institute>(){ new Institute{Id=1, Name= "[12345] saleha ideal school"}, new Institute{Id=2, Name= "[456987] ideal school & college"}, new I

我有一份如下机构名单:

    List<Institute> instituteList = new List<Institute>(){
      new Institute{Id=1, Name= "[12345] saleha ideal school"},
      new Institute{Id=2, Name= "[456987] ideal school & college"},
      new Institute{Id=3, Name= "[698745] dhaka faizul islam ideal school"},
      new Institute{Id=4, Name= "[596314] nurul haque ideal school"}
    }
我尝试了以下两种方法:

instituteList = instituteList.OrderByDescending(x => x.Name.Contains("ideal")).ToList();
instituteList = instituteList.OrderBy(i => "ideal".IndexOf(i.Name)).ToList();

但是没有人在工作。

你的工作几乎在那里,但不完全在那里。试试这个:

instituteList.Sort((a, b) => a.Name.ToLower().IndexOf(query.ToLower()).CompareTo(b.Name.ToLower().IndexOf(query.ToLower())));

注意:从我的手机上写的,可能有打字错误…

您的
机构列表
初始值设定项将无法编译。另外,使用
Sort
方法看起来更自然,而不是在排序后重新分配列表使用Sort方法有效@Pavelanikhouski InstituteList.Sort((a,b)=>a.Name.ToLower().IndexOf(query.ToLower()).CompareTo(b.Name.ToLower().IndexOf(query.ToLower()));成功了@Zohar PeledOf当然,我真傻,竟然忘了财产这个名字。。。将在tomorrowGlad中编辑以提供帮助;-)
instituteList.Sort((a, b) => a.Name.ToLower().IndexOf(query.ToLower()).CompareTo(b.Name.ToLower().IndexOf(query.ToLower())));