C#按字母顺序对字符串数组排序,注意将以大写字母开头的字符串放在第一位。第一

C#按字母顺序对字符串数组排序,注意将以大写字母开头的字符串放在第一位。第一,c#,arrays,string,sorting,alphabetical,C#,Arrays,String,Sorting,Alphabetical,1) 有一个Kata,表示对字符串数组中的所有字符串进行排序,然后取第一个单词并在每个字母之间添加***: 2) 例如: (1) 它被给出: bitcoin take over the world maybe who knows perhaps (2) 订购后: bitcoin knows maybe over perhaps take the who world all cold go holiday Lets on somewhere very (3) 返回的结果是: b***i***

1) 有一个Kata,表示对字符串数组中的所有字符串进行排序,然后取第一个单词并在每个字母之间添加***:

2) 例如:

(1) 它被给出:

bitcoin
take
over
the
world
maybe
who
knows
perhaps
(2) 订购后:

bitcoin
knows
maybe
over
perhaps
take
the
who
world
all
cold
go
holiday
Lets
on
somewhere
very
(3) 返回的结果是:

b***i***t***c***o***i***n
3) 然而,我面临的困难是:我们如何表达“以大写字母开头的单词优先顺序”

4) 我尝试了以下代码:

using System;
public class Kata
{
  public static string TwoSort(string[] s)
  {
  foreach(string str in s){
    Console.WriteLine(str);
  }
  Console.WriteLine("");
  Array.Sort(s);
  foreach(string str in s){
    Console.WriteLine(str);
  }

  Console.WriteLine("");

  string firstWord = s[0];
  string result = "";


  foreach(char letter in firstWord){
    result += letter + "***";

  }
  Console.WriteLine(result.Substring(0, result.Length - 3));
    return result.Substring(0, result.Length - 3);
  }  
}
5) 例如:

(1) 它被赋予以下数组:

Lets
all
go
on
holiday
somewhere
very
cold
(2) 订购后:

bitcoin
knows
maybe
over
perhaps
take
the
who
world
all
cold
go
holiday
Lets
on
somewhere
very
(3) 当前错误结果:

a***l***l
(4) 预期正确结果:

L***e***t***s
我也读过:

您应该指定比较器,例如(Linq解决方案):

为了获得期望的结果(如果您坚持订购),您可以

  var result = string.Join("***", source
    .OrderBy(item => item, StringComparer.Ordinal) 
    .First()
    .Select(c => c)); // <- turn string into IEnumerable<char> 

  Console.Write(result);
如果您想继续使用当前代码,请更改
Array.Sort进入

  Array.Sort(s, StringComparer.Ordinal);

您可以指定序数字符串比较器,以先大写字母,然后小写字母来缩短结果

Array.Sort(s, StringComparer.Ordinal);
Array.Sort(s, StringComparer.Ordinal);