我有一个字符串,简化为"12345"
它是排序的。字符串couild包含数字(0-9)或字母(a-z)。如果混合使用自然排序顺序。我需要一种方法来验证这是否属实。检查一个字符串是否排序
尝试使用LINQ技术:
string items1 = "2349"; //sorted
string items2 = "2476"; //not sorted, 6<>7
bool sorted1 = Enumerable.SequenceEqual(items1.OrderBy(x => x), items1); //true
bool sorted2 = Enumerable.SequenceEqual(items2.OrderBy(x => x), items2); //false
但可能还按降序排序。
是否有更好的方法,然后
string items3 = "4321";
bool sorted3 = Enumerable.SequenceEqual(items3.OrderBy(x => x), items3) || Enumerable.SequenceEqual(items3.OrderByDescending(x => x), items3);
检查一个字符串排序?也许有些内置解决方案?
http://stackoverflow.com/questions/6396378/c-sharp-linq-orderby-numbers-that-are-string-and-you-cannot-convert-them-to-int – Oluwafemi
如果字符串包含,该怎么办你的信?您刚刚向我们展示了最佳案例场景。如果字符不是英文字母(例如'-'或'?')? – user3185569
@ c0rd因此'123abc'和'abc123'和'ab12cd34'被认为是分类的? – user3185569