字符串列表排序版本 [英] Sort List of Strings By Version

查看:123
本文介绍了字符串列表排序版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串列表。每个字符串都遵循模式{Path} \UpdateTo {Version} - {Order}。

I have a list of strings. Each string follows the pattern "{Path}\UpdateTo{Version}-{Order}".

我需要对列表进行排序,顶端。在存在具有相同版本号的多个文件的情况下,则附加可选的顺序参数。如果一个订单存在于任何字符串,那么它应该出现在没有订单号的相同版本号的字符串之上。

I need to sort the list such that the lowest version numbers are at the top. In the case when there is multiple files with the same version number then an optional order parameter is appended. If an order is present on any of the strings then it should appear above the strings with the same version number that do not have an order number.

例如,给(注意项目是随机排序的):

For example, give the following list (note the items are randomly ordered):

var files = new List<string>() {
    @"C:\Migrations\UpdateTo1.2-2",
    @"C:\Migrations\UpdateTo1.5-2",
    @"C:\Migrations\UpdateTo1.2",
    @"C:\Migrations\UpdateTo1.4",
    @"C:\Migrations\UpdateTo1.1",
    @"C:\Migrations\UpdateTo1.5",
    @"C:\Migrations\UpdateTo1.2-1",
    @"C:\Migrations\UpdateTo1.5-1"
};

结果将是:

var files = new List<string>() {
    @"C:\Migrations\UpdateTo1.1",
    @"C:\Migrations\UpdateTo1.2-1",
    @"C:\Migrations\UpdateTo1.2-2",
    @"C:\Migrations\UpdateTo1.2",
    @"C:\Migrations\UpdateTo1.4",
    @"C:\Migrations\UpdateTo1.5-1",
    @"C:\Migrations\UpdateTo1.5-2",
    @"C:\Migrations\UpdateTo1.5"
}


$ b b

我一直在尝试各种各样的想法,但到目前为止我的尝试是一个完整的混乱。我会感激,如果有人可以帮助。感谢

I've been trying with all sorts of ideas but so far my attempts have been a complete mess. I'd appreciate it if someone could help. Thanks

推荐答案

我使用一个临时类来处理解析和比较以获得所需的输出。我已经包括了代码,让一切返回到你所要求的,但临时类介绍可能有更多的价值,只有路径(?)。

I used a temporary class to handle the parsing and the comparisons to get the desired output. I've included code that gets everything back to how you requested it but the "temporary" class introduced may have more value to you over just the paths (?).

用法:

var sorted = files.Select(f => new UpdateTo(f))
    .OrderBy(u => u)
    .Select(u => u.Path)
    .ToArray();

代码:

class UpdateTo : IComparable<UpdateTo>
{
    public decimal Version { get; private set; }
    public int Order { get; private set; }
    public string Path { get; private set; }

    private const string Prefix = "UpdateTo";

    public UpdateTo(string path)
    {
        /* No error-checking here -- BEWARE!! */
        Path = path;

        string toParse = Path.Substring(Path.IndexOf(Prefix, StringComparison.InvariantCultureIgnoreCase) + Prefix.Length);
        var split = toParse.Split('-');

        Version = decimal.Parse(split[0]);
        Order = split.Length == 2 ? int.Parse(split[1]) : int.MaxValue;
    }

    public int CompareTo(UpdateTo other)
    {
        int versionCompare = Version.CompareTo(other.Version);
        return versionCompare == 0 ? Order.CompareTo(other.Order) : versionCompare;
    }
}

并且测试...

[Test]
public void ListSort()
{
    const string first = @"C:\Migrations\UpdateTo1.1";
    const string second = @"C:\Migrations\UpdateTo1.2-1";
    const string third = @"C:\Migrations\UpdateTo1.2-2";
    const string fourth = @"C:\Migrations\UpdateTo1.2";
    const string fifth = @"C:\Migrations\UpdateTo1.4";
    const string sixth = @"C:\Migrations\UpdateTo1.5-1";
    const string seventh = @"C:\Migrations\UpdateTo1.5-2";
    const string eighth = @"C:\Migrations\UpdateTo1.5";

    var files = new List<string>{third, seventh, fourth, fifth, first, eighth, second, sixth};

    var sorted = files.Select(f => new UpdateTo(f))
        .OrderBy(u => u)
        .Select(u => u.Path)
        .ToArray();

    Assert.AreEqual(first, sorted[0]);
    Assert.AreEqual(second, sorted[1]);
    Assert.AreEqual(third, sorted[2]);
    Assert.AreEqual(fourth, sorted[3]);
    Assert.AreEqual(fifth, sorted[4]);
    Assert.AreEqual(sixth, sorted[5]);
    Assert.AreEqual(seventh, sorted[6]);
    Assert.AreEqual(eighth, sorted[7]);
}

这篇关于字符串列表排序版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆