代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class VersionSorter : IComparer<string>
{
public int Compare(string x, string y)
{
Version verX = new Version(x);
Version verY = new Version(y);
return verX.CompareTo(verY);
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<string> versions = new List<string> { "1.1.0", "2.5", "2.1.5", "2.0", "10.1" };
versions.Sort(new VersionSorter());
foreach (var version in versions)
{
Console.WriteLine(version);
}
}
}
}
输出结果:
1.1.0
2.0
2.1.5
2.5
10.1