C#中字符串按长度排序怎么编写代码?

2024-12-02 14:57:56
推荐回答(2个)
回答1:

string[] Mystrings = new string(){};
//把需要排序的字符串添加搭配Mystring里
...
//开始排序
for (int i = 0; i < Mystrings.Count(); i++) //每个字符串都要参与比较
{
for (int j = 1; j < Mystrings.Count(); j++) //字符串长度较长的排在前面
{
if (Mystrings[j - 1].Length < Mystrings[j].Length)
{
string temp = Mystrings[j - 1];
Mystrings[j - 1] = Mystrings[j];
Mystrings[j] = temp;
}
}
}

回答2:

判断字符串的length属性,然後用冒泡排序法排序就OK了