我有一个字符串列表如下所示。我想根据Start with值对名称进行排序。我尝试了一些案例,但失败了。例子列表中有像
这样的数据name: "Bible Reading",
name: "Scripture Reading",
name: "Ready Set",
name: "Career Readings",
name: "Reading-Berks"
我传递queryValue为"read",但我不能根据值排序。
IOrderedEnumerable<string> name = result.Select(x => x.name)
.OrderBy(i => i.StartsWith(queryValue));
期望输出,
name: "Ready Set",
name: "Reading-Berks"
name: "Bible Reading",
name: "Scripture Reading",
name: "Career Readings",
我认为这是一个区分大小写的问题。
代替.OrderBy(i => i.StartsWith(queryValue));
.OrderByDescending(i => i.StartsWith(queryValue, StringComparison.InvariantCultureIgnoreCase));
因为"Ready Set".StartsWith("read")
将返回false
您必须对所需的结果使用OrderByDescending
和不区分大小写的比较,因此您的查询将如下所示:
IOrderedEnumerable<string> name = result.Select(x => x.name)
.OrderByDescending(i =>i.StartsWith(queryValue,StringComparison.InvariantCultureIgnoreCase));
下面是一个区分OrderByDescending
和OrderBy
的工作示例