首先按数字排序

时间:2016-01-02 07:00:18

标签: c# .net string linq

我有

下的列表
 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

我该怎么办?

4 个答案:

答案 0 :(得分:5)

您看到和预期的输出基于OriginalString,因为ExtractNumbers"","","1","2",那么您应该OrderBy OriginalString

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();

输出:

  

1

     

2

     

a

     

B'/ P>

答案 1 :(得分: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

P.S:你没有告诉我们关于你的逻辑的任何其他解释String.Join("", item.Where(Char.IsDigit)。所以,我没有问过任何其他问题。

答案 2 :(得分:1)

如果您的列表仅包含1-char字符串,则可以按其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

答案 3 :(得分:0)

这个解决方案可能看起来很复杂,但它适用于任何Net内置的排序方法。

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;
        }
    }
}