2016-01-02 86 views
2

數字我有一個列表,下排序第一

var x = new List<string>() { "a", "1", "b", "2" }; 
var sortedResultByNumeric = x 
       .Select(s => new { OriginalString = s, ExtractNumbers = String.Join("", s.Where(Char.IsDigit)) }) 
       .OrderBy(o => o.ExtractNumbers).ToList(); 

輸出是

a 
b 
1 
2 

但預期是

1 
2 
a 
b 

我該怎麼辦呢?

回答

1

如果列表中只包含1字符的字符串,你可以通過它們的char數值命令他們:

var x = new List<string>() { "a", "1", "b", "2" }; 
var sorted = x.OrderBy(c => Convert.ToChar(c)) 
       .ToList(); 

foreach (var c in sorted) 
    Console.WriteLine(c); 

// 1 
// 2 
// a 
// b 
2

可以嘗試這樣:

var myList = new List<string>() { "a", "1", "b", "2", "123", "cd", "12346", "657" }; 

var nonNumericItems = myList.Where(item => !item.Any(i => Char.IsDigit(i))) 
     .OrderBy(item => item); 

var numericItems = myList 
     .Select(item => String.Join("", item.Where(Char.IsDigit))) 
     .Where(item => !String.IsNullOrEmpty(item)) 
     .Select(item => Convert.ToInt32(item)) 
     .OrderBy(item => item) 
     .Select(item => item.ToString()); 

var result = numericItems 
     .Union(nonNumericItems); 

result.ToList() 
     .ForEach(Console.WriteLine); 

的輸出是:

1,2,123,657,12346,A,B,CD

PS:你在這裏沒有告訴我們關於你的邏輯的任何額外的解釋String.Join("", item.Where(Char.IsDigit)。所以,我沒有提出任何其他問題。

5

,你看到和預期是基於OriginalString因爲ExtractNumbers"","","1","2"那麼你應該OrderByOriginalString輸出:

var x = new List<string>() { "a", "1", "b", "2" }; 
var sortedResultByNumeric = x 
      .Select(s => new { OriginalString = s, ExtractNumbers = String.Join("", s.Where(Char.IsDigit)) }) 
      .OrderBy(o => o.OriginalString).ToList(); 

輸出:

a

b

0

這種解決方案可能看起來複雜,但它會與內置的排序方法的任何淨功。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 


namespace ConsoleApplication63 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.csv"; 
     static void Main(string[] args) 
     { 
      List<AlphaNumberic> aN = new List<AlphaNumberic>() { "a", "1", "b", "2" }; 
      aN.Sort((x, y) => x.CompareTo(y)); 
     } 
    } 
    public class AlphaNumberic 
    { 
     int results = 0; 
     public string alphaNumeric { get; set; } 
     public AlphaNumberic(string value) 
     { 
      this.alphaNumeric = value; 
     } 
     public static implicit operator string(AlphaNumberic d) 
     { 
      return d.alphaNumeric; 
     } 
     public static implicit operator AlphaNumberic(string d) 
     { 
      return new AlphaNumberic(d); 
     } 
     public int CompareTo(AlphaNumberic compareItem) 
     { 
      int thisNum; 
      int num; 
      if (int.TryParse(compareItem.alphaNumeric, out num)) 
      { 
       if (int.TryParse(this.alphaNumeric, out thisNum)) 
       { 
        //alphaNumeric and compareItem both integers; 
        results = thisNum.CompareTo(num); 
       } 
       else 
       { 
        //alphaNumeric not an integer and compareItem integers; 
        results = 1; 
       } 
      } 
      else 
      { 
       if (int.TryParse(this.alphaNumeric, out thisNum)) 
       { 
        //alphaNumeric is an integer and compareItem not an integers; 
        results = -1; 
       } 
       else 
       { 
        //alphaNumeric not an integer and compareItem not an integers; 
        results = alphaNumeric.CompareTo(compareItem); 
       } 
      } 
      return results; 
     } 
    } 
}