如何对列表<string>/字符串版本号数组进行排序?
How to sort a list<string> / array of string version number?
我有一个版本字符串列表(见图),我想按降序对它们进行排序。
我见过一些使用版本 class 来比较它们的解决方案,但我想不出有任何解决方案可以像这样对整个列表进行排序。实现这一目标最简单的方法是什么?
这个简单的实现有什么问题?
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();
}
}
}
你可以使用 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;
}
}
}
您应该使用 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;
}
这里有一个解决方案,可以对语义版本控制 (https://semver.org) 进行排序。 Nuget.Core 提供了一个 SemanticVersion class,其操作与 .net 中的标准版本 class 非常相似。它会将您的字符串解析为 IComparable 和 IEquatable 对象,以便您可以比较多个版本或在集合中对它们进行排序等。
Nuget.Core: https://www.nuget.org/packages/nuget.core/(你可以通过 nuget 拉取这个库)
https://github.com/NuGet/NuGet2/blob/2.13/src/Core/SemanticVersion.cs
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();
知道这是一个旧话题,但最简单的方法是:
//list with some version numbers as strings
var versionList = new List<string>()
{
"1.1.0",
"1.10.0",
"1.112.0",
}
versionList.OrderByDescending(x => Version.Parse(x));
输出:
1.112.0
1.10.0
1.1.0
我有一个版本字符串列表(见图),我想按降序对它们进行排序。
我见过一些使用版本 class 来比较它们的解决方案,但我想不出有任何解决方案可以像这样对整个列表进行排序。实现这一目标最简单的方法是什么?
这个简单的实现有什么问题?
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();
}
}
}
你可以使用 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;
}
}
}
您应该使用 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;
}
这里有一个解决方案,可以对语义版本控制 (https://semver.org) 进行排序。 Nuget.Core 提供了一个 SemanticVersion class,其操作与 .net 中的标准版本 class 非常相似。它会将您的字符串解析为 IComparable 和 IEquatable 对象,以便您可以比较多个版本或在集合中对它们进行排序等。
Nuget.Core: https://www.nuget.org/packages/nuget.core/(你可以通过 nuget 拉取这个库)
https://github.com/NuGet/NuGet2/blob/2.13/src/Core/SemanticVersion.cs
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();
知道这是一个旧话题,但最简单的方法是:
//list with some version numbers as strings
var versionList = new List<string>()
{
"1.1.0",
"1.10.0",
"1.112.0",
}
versionList.OrderByDescending(x => Version.Parse(x));
输出:
1.112.0
1.10.0
1.1.0