排序数字字符串映射字符串

时间:2014-05-26 07:39:29

标签: c# sorting natural-sort

我们有关排序字符串的问题。排序在这种情况下不起作用

1-A 1-B 10-A 10-B 9-A 9-B ......

我们真正想要的是

1-A 1-B 9-A 9-B 10-A 10-B  如果我们将它排序为字符串,结果将类似于第一部分。如果我们解析然后排序那么我们如何对其余的A和B进行排序? c#有什么简单的解决方案吗?

感谢;

7 个答案:

答案 0 :(得分:2)

您可以implement own comparer放置自定义比较逻辑。然后你可以use this comparer instance进行排序。

答案 1 :(得分:1)

我建议使用IComparer

很容易实现和排序,例如。

1.txt
10.txt
3.txt
a10b1.txt
a1b1.txt
a2b1.txt
a2b11.txt
a2b2.txt
b1.txt
b10.txt
b2.txt  

排序为

1.txt
3.txt
10.txt
a1b1.txt
a2b1.txt
a2b2.txt
a2b11.txt
a10b1.txt
b1.txt
b2.txt
b10.txt

答案 2 :(得分:1)

只需使用以下代码

string[] strArr = new string[] {"1-A", "1-B", "9-A", "9-B", "10-A", "10-B"};
var q = from t in strArr select new { FirstPart =Convert.ToInt32(t.Split('-')[0]), SecondPart = t.Split('-')[1] };

string[] resArr = q.OrderBy(p => p.FirstPart).ThenBy(p => p.SecondPart).Select(p=> string.Concat(p.FirstPart, "-", p.SecondPart)) .ToArray();

答案 3 :(得分:1)

一种方法是,你可以使用LINQ' OrderBy + ThenBy

string[] strings = {"1-A", "1-B", "10-A", "10-B", "9-A", "9-B"}; 
int i = 0;
var orderedStrings = strings
   .Select(str => new { arr = str.Split('-'), str })
   .Where(x => x.arr.Length == 2 && int.TryParse(x.arr[0], out i))
   .Select(x => new { x.str, x.arr, num = int.Parse(x.arr[0])})
   .OrderBy(x => x.num)
   .ThenBy(x => x.arr[1])
   .Select(x => x.str); 

假设字符串始终由-分隔,包含两部分,第一部分始终为int。使用Where确保这一点,省略了格式无效的字符串。

如果要将已排序的查询重新分配给string[],则需要创建一个新查询:

strings = orderedStrings.ToArray();

答案 4 :(得分:1)

您正在寻找“自然排序”。

事实证明我们可以使用P / Invoke调用the Windows API provides a StrCmpLogicalW() function来解决问题。

假设您正在尝试对List<>或数组进行排序,可以将其包装在扩展方法中,以便更容易调用:

public static class ListAndArrayExt
{
    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
    private static extern int StrCmpLogicalW(string lhs, string rhs);

    public static void SortNatural(this List<string> self)
    {
        self.Sort(StrCmpLogicalW);
    }

    public static void SortNatural(this string[] self)
    {
        Array.Sort(self, StrCmpLogicalW);
    }
}

然后,您可以使用它按自然排序顺序对List<>进行排序。这是一个完整的可编辑控制台应用程序,用于演示:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    public static class ListAndArrayExt
    {
        [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
        private static extern int StrCmpLogicalW(string lhs, string rhs);

        public static void SortNatural(this List<string> self)
        {
            self.Sort(StrCmpLogicalW);
        }

        public static void SortNatural(this string[] self)
        {
            Array.Sort(self, StrCmpLogicalW);
        }
    }

    class Program
    {
        void run()
        {
            var strings = new List<string>
            {
                "1-A",
                "1-B",
                "10-A",
                "10-B",
                "9-A",
                "9-B"
            };

            strings.SortNatural();

            foreach (var s in strings)
                Console.WriteLine(s);
        }

        static void Main()
        {
            new Program().run();
        }
    }
}

这样可以打印出字符串:

1-A
1-B
9-A
9-B
10-A
10-B

答案 5 :(得分:0)

你需要实现自定义比较器,它将根据你的意愿主持字符串。例如NaturalComparer

答案 6 :(得分:-2)

尝试使用

  

的ArrayList

然后使用

  

ArrayList.Sort();

它将对您的数组进行排序。

相关问题