C# 如何按升序对字母数字文件名数组进行排序?

C# 如何按升序对字母数字文件名数组进行排序?,c#,C#,我有一个.sql目录文件字符串数组,需要按升序排序。实际上,我想根据数字部分对其进行排序,如果没有数字部分,则文件应该位于顶部。 我该怎么做 3.5.1_Patch 3.5_CoreScript 3.6_Patch 3.6.1_Patch 你应该让你的问题尽可能具体。您可以使用Array.Sort和lambda函数执行任何操作: string [] files = new string[] {"3.6.1_Patch","3.5_CoreScript","3.5.1_Patch","3.

我有一个.sql目录文件字符串数组,需要按升序排序。实际上,我想根据数字部分对其进行排序,如果没有数字部分,则文件应该位于顶部。 我该怎么做

3.5.1_Patch

3.5_CoreScript

3.6_Patch

3.6.1_Patch

你应该让你的问题尽可能具体。您可以使用
Array.Sort
和lambda函数执行任何操作:

string [] files = new string[] {"3.6.1_Patch","3.5_CoreScript","3.5.1_Patch","3.6_Patch"};
Array.Sort(files, (x,y) => {
    string [] x1 = x.Split('_');
    string [] y1 = y.Split('_');
    return String.Compare(x1[0], y1[0]);
});
我让你来处理没有版本号的edge案例

编辑

您的文件名比最初显示的更复杂。我假设第一个非数字字符串之前的所有内容都是版本

此时,我将创建一个类来解析文件名并存储版本号。它还实现了排序的
IComparable
接口

public class Version : IComparable<Version> {
    // Just guessing here - without knowing the actual format
    public int Major = 0;
    public int Minor = 0;
    public int Build = 0;
    public string FileName;

    public Version(string fileName) {
        ParseFileName(fileName);
    }

    // Split the string on '_' or '.',
    // Considers the first 3 numbers to be version
    // (stops parsing at non-numeric value)
    public void ParseFileName(string fileName) 
    {
        FileName = fileName;
        string [] data = Regex.Split(fileName, @"[_\.]");
        int x;
        if (Int32.TryParse(data[0], out x)) {
            Major = x;
            if (2 <= data.Length && Int32.TryParse(data[1], out x)) {
                Minor = x;
                if (3 <= data.Length && Int32.TryParse(data[2], out x)) {
                    Build = x;
                }
            }
        }
    }

    public override string ToString() {
        return FileName;
    }

    // Comparison
    public int CompareTo(Version v) {
        int c = Major.CompareTo(v.Major);
        if (0 == c) {
            c = Minor.CompareTo(v.Minor);
        }
        if (0 == c) {
            c = Build.CompareTo(v.Build);
        }
        return c;
    }
}
试试这个:

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

namespace ConsoleApplication34
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> input = new List<string>() { "3.5.1_Patch", "3.5_CoreScript", "3.6_Patch", "3.6.1_Patch" };


            input.Sort((x,y) => new CustomSort() { s = x}.CompareTo(y));
        }
    }

    public class CustomSort : IComparable
    {
        public string s { get; set; }
        public int CompareTo(object input)
        {
            string[] thissplitArray = (s).Split(new char[] { '_' }).ToArray();
            string[] splitArray = ((string)input).Split(new char[] { '_' }).ToArray();

            if (thissplitArray[0] == splitArray[0])
            {
                return thissplitArray[1].CompareTo(splitArray[1]);
            }
            else
            {
                return thissplitArray[0].CompareTo(splitArray[0]);
            }

        }
    }


}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序34
{
班级计划
{
静态void Main(字符串[]参数)
{
列表输入=新列表(){“3.5.1_补丁”、“3.5_核心脚本”、“3.6_补丁”、“3.6.1_补丁”};
输入.Sort((x,y)=>newcustomsort(){s=x}.CompareTo(y));
}
}
公共类CustomSort:IComparable
{
公共字符串s{get;set;}
公共整数比较(对象输入)
{
字符串[]thissplitArray=(s).Split(新字符[]{'.}).ToArray();
string[]splitArray=((字符串)输入).Split(新字符[]{'.}).ToArray();
if(thissplitArray[0]==splitArray[0])
{
返回此splitArray[1]。比较(splitArray[1]);
}
其他的
{
返回thissplitArray[0]。比较(splitArray[0]);
}
}
}
}

是的,我问过你,但不确定这个字母数字部分。你应该展示你尝试过的内容,否则你的问题会因为缺乏努力而被否决,可能会被关闭。此外,您还应该详细说明您希望如何进行排序。我假设您希望按下划线前的数字和下划线后的文本对版本进行排序,但是如果有一个文件没有版本号,那该怎么办呢?这将表示2大于10。@Servy True。Will fix…@Johnnymop如果涉及多个问题,我将如何排序?像01_1_ALTER_TC_EDB_V3、01_2_ALTER_TC_EDB_V3一样,您将需要更高级的解析。您的示例中的版本字符串是什么?
test
01_1_ALTER_TC_EDB_V3
01_2_ALTER_TC_EDB_V3
3.5_CoreScript
3.5.1_Patch
3.6_Patch
10.6_Patch
10.6.1_Patch
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication34
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> input = new List<string>() { "3.5.1_Patch", "3.5_CoreScript", "3.6_Patch", "3.6.1_Patch" };


            input.Sort((x,y) => new CustomSort() { s = x}.CompareTo(y));
        }
    }

    public class CustomSort : IComparable
    {
        public string s { get; set; }
        public int CompareTo(object input)
        {
            string[] thissplitArray = (s).Split(new char[] { '_' }).ToArray();
            string[] splitArray = ((string)input).Split(new char[] { '_' }).ToArray();

            if (thissplitArray[0] == splitArray[0])
            {
                return thissplitArray[1].CompareTo(splitArray[1]);
            }
            else
            {
                return thissplitArray[0].CompareTo(splitArray[0]);
            }

        }
    }


}