如何对列表<string> /数组的字符串版本号进行排序?

时间:2016-06-02 00:25:02

标签: c# arrays list sorting

我有一个版本的字符串列表(见照片),我想按降序对它们进行排序。

enter image description here

我已经看过一些使用Version类进行比较的解决方案,但是我无法想到任何解决方案,这样可以对整个列表进行排序。实现这一目标的最简单方法是什么?

6 个答案:

答案 0 :(得分:10)

这个简单的实现有什么问题?

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var ver = new List<Version>();

            ver.Add(new Version("3.5"));
            ver.Add(new Version("3.15"));
            ver.Add(new Version("3.10"));
            ver.Add(new Version("3.1"));

            ver.Sort();
            ver.Reverse();
        }
    }
}

答案 1 :(得分:6)

OrderBy中显示的标准排序解决方案Version应该在这种情况下工作,您要求它按 var listOfStrings = new List<string>{"1.2", "2.3", "0.1"}; listOfStrings = listOfStrings.OrderBy(x => new Version(x)).ToList(); 订购:

{{1}}

答案 2 :(得分:3)

您可以使用IComparable

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
              List<string> data = new List<string>{
                "3.5.0.1", "3.4.1.9", "3.4.1.56", "3.4.1.55", "3.4.1.46",
                "3.4.1.45", "3.4.1.44", "3.4.1.30", "3.4.1.3", "3.4.1.22",
                "3.4.1.2", "3.4.1.11", "3.4.1.0", "3.4.0.7", "3.4.0.3",
                "3.4.0.1", "3.3.0.8", "3.3.0.4", "3.3.0.0", "3.2.0.9",
                "3.2.0.6", "3.2.0.3", "3.2.0.27", "3.2.0.20", "3.2.0.15",
                "3.2.0.1", "3.2.0.0", "3.1.0.7", "3.1.0.15", "3.1.0.14"
              };
              List<SortPara> sortPara = data.Select(x => new SortPara(x)).ToList();
              data = sortPara.OrderBy(x => x).Select(x => x.strNumbers).ToList();
              data = sortPara.OrderByDescending(x => x).Select(x => x.strNumbers).ToList();
        }

    }
    public class SortPara : IComparable<SortPara>
    {
        public List<int> numbers { get; set; }
        public string strNumbers { get; set; }
        public SortPara(string strNumbers)
        {
            this.strNumbers = strNumbers;
            numbers = strNumbers.Split(new char[] { '.' }).Select(x => int.Parse(x)).ToList();

        }
        public int CompareTo(SortPara other)
        {
            int shortest = this.numbers.Count < other.numbers.Count ? this.numbers.Count : other.numbers.Count;
            int results = 0;
            for (int i = 0; i < shortest; i++)
            {
                if (this.numbers[i] != other.numbers[i])
                {
                    results = this.numbers[i].CompareTo(other.numbers[i]);
                    break;
                }
            }
            return results;
        }
    }
}

答案 3 :(得分:1)

你应该使用IComparable作为jdweng,只需编辑一下比较“2.1.0.4”和“2.1”之类的版本:

public int CompareTo(SortPara other)
    {
        int shortest = this.numbers.Count < other.numbers.Count ? this.numbers.Count : other.numbers.Count;
        int results = 0;
        for (int i = 0; i < shortest; i++)
        {
            if (this.numbers[i] != other.numbers[i])
            {
                results = this.numbers[i].CompareTo(other.numbers[i]);
                break;
            }
        }
        if (results != 0)
            return results;
        if (this.numbers.Count > other.numbers.Count)
            return 1;
        else if (this.numbers.Count < other.numbers.Count)
            return -1;
        else
            return 0;
    }

答案 4 :(得分:0)

这是一个对语义版本控制(enter image description here)进行排序的解决方案。 Nuget.Core 提供了一个 SemanticVersion 类,其操作与.net中的标准Version类非常相似。它将字符串解析为IComparable和IEquatable对象,以便您可以比较多个版本或在集合中对它们进行排序等。

Nuget.Core:https://semver.org(您可以通过nuget提取此库)

https://www.nuget.org/packages/nuget.core/

var rawVersions = new [] {"v1.4.0", "v1.4.0-patch10", "v1.4.0-patch2"};
var versions = rawVersions.Select(v => new SemanticVersion(v));
var sorted = versions.ToList().Sort();

答案 5 :(得分:-4)

关键是要思考数字

转换

  • 0至a
  • 1到b
  • 2到c

然后您可以排序