C#排序字符串小写和大写字母 [英] C# sorting strings small and capital letters
本文介绍了C#排序字符串小写和大写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否存在标准功能,该功能允许我按以下方式对大写字母和小写字母进行排序,否则我应该实现自定义比较器:
Is there a standard functionality which will allow me to sort capital and small letters in the following way or I should implement a custom comparator:
student
students
Student
Students
对于实例:
using System;
using System.Collections.Generic;
namespace Dela.Mono.Examples
{
public class HelloWorld
{
public static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("student");
list.Add("students");
list.Add("Student");
list.Add("Students");
list.Sort();
for (int i=0; i < list.Count; i++)
Console.WriteLine(list[i]);
}
}
}
它将字符串排序为:
student
Student
students
Students
如果我尝试使用list.Sort(StringComparer.Ordinal)
,则排序如下:
If I try to use list.Sort(StringComparer.Ordinal)
, the sorting goes like:
Student
Students
student
students
推荐答案
您在这些行上是不是有意思
Do you mean something on these lines
List<string> sort = new List<string>() { "student", "Students", "students",
"Student" };
List<string> custsort=sort.OrderByDescending(st => st[0]).ThenBy(s => s.Length)
.ToList();
第一个按第一个字符排序,然后按长度排序. 按照我上面提到的模式,它匹配您建议的输出,否则您将执行一些自定义比较器
The first one orders it by the first character and then by the length. It matches the output you suggested by then as per the pattern i mentioned above else you will do some custom comparer
这篇关于C#排序字符串小写和大写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文