简体   繁体   English

名单 <string> 不排序

[英]List<string> not sorting

The string is returned the same as it enters the function: 返回的字符串与输入函数相同:

  public static string AlphaSort(string S)
      {
        var NewS = new List<string>();
        var Arr = S.Replace(" ", "").Split();
        NewS = Arr.ToList();
        var SortedS = NewS.OrderBy(c => c).ToList();
        return String.Join("", SortedS.ToArray());
      }

I have also tried NewS.Sort() to no avail 我也尝试过NewS.Sort()无济于事

I think you're overthinking things. 我认为您想得太多。 Try something like: 尝试类似:

public static string AlphaSort( string s )
{
  string sorted = string.Join( "" ,
                    ( s ?? "")
                    .Split()
                    .OrderBy( x => x )
                    ) ;
  return sorted ;
}

which turns this string 变成这个字符串

"The    Quick Brown Fox      Jumped Over      The Lazy      Dog"

into this string 到这串

"BrownDogFoxJumpedLazyOverQuickTheThe"

If you're trying to order the individual characters, it gets much easier. 如果您要订购单个字符,它将变得更加容易。 A string is an IEnumerable<char> (very convenient). stringIEnumerable<char> (非常方便)。 It's also readily convertable to a char[] (since that's ultimately its backing store). 它也可以很容易地转换为char[] (因为这最终是它的后备存储)。 In this case, something like 在这种情况下,

public static string AlphaSort( string s )
{
  string sorted = new string(
                    (s ?? "")
                    .Where( c => !char.IsWhiteSpace(c) )
                    .OrderBy( c => c )
                    );
  return sorted ;
}

Given the input noted above, 鉴于上述输入,

"The    Quick Brown Fox      Jumped Over      The Lazy      Dog"

this produces the string 这产生了字符串

"BDFJLOQTTacdeeeeghhikmnoooprruuvwxyz"

One might note that once you're looking at characters, the ordering is no longer really alphabetic. 可能有人会注意到,一旦您查看字符,该顺序就不再是真正的字母顺序了。 Rather, it's an ordinal sort, since a char is an unsigned short. 相反,这是一种序数排序,因为char是一个无符号的short。

Here's another approach 这是另一种方法

public static string AlphaSort( string s )
{
  char[] chars = s.ToCharArray() ;

  Array.Sort( chars ) ;

  int i = 0 ;
  while ( i < chars.Length && char.IsWhiteSpace(chars[i]) ) ++i ;

  string sorted = new string( chars , i , chars.Length-i ) ;
  return sorted ;
}

producing the same 生产相同

"BDFJLOQTTacdeeeeghhikmnoooprruuvwxyz"

from

"The    Quick Brown Fox      Jumped Over      The Lazy      Dog"

Cheers! 干杯!

I think you're trying to take a string and return the characters alphabetically. 我认为您正在尝试采用字符串并按字母顺序返回字符。 If so, try this 如果是这样,请尝试

public static string AlphaSort(string S)
{
    var Arr = S.Replace(" ", "").ToCharArray();
    var SortedS = Arr.OrderBy(c => c);
    return String.Join("", SortedS);
}
public static string AlphaSort(string S)
  {        
    var Arr = S.Replace(" ", "").Split(' ');
    var NewS = Arr.ToList();
    var SortedS = NewS.OrderBy(c => c).ToList();
    return String.Join("", SortedS.ToArray());
  }

or simply you could use without using Replace: 或者您可以不使用Replace即可使用:

var Arr = S.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries)

This will return the string sorted case insensitive 这将返回不区分大小写的字符串排序

public static string AlphaSort(string S)
{
    string[] NewS = S.Split(' ');
    Array.Sort(NewS, new CaseInsensitiveComparer());
    return String.Join(" ", NewS);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM