'How to sort a list<string> / array of string version number?

I have a list of strings of version (see photo), and I'd like to sort them in descending order.

enter image description here

I've seen a few solutions using Version class to compare them, but I can't think of any solution that sort a whole list like this. What is the least complicated way to achieve this?



Solution 1:[1]

what is wrong with this simple implementation?

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();
        }
    }
}

Solution 2:[2]

you can use 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;
        }
    }
}

Solution 3:[3]

You should use IComparable as jdweng, just edit a bit to compare versions like "2.1.0.4" and "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;
    }

Solution 4:[4]

know its an old thread but the easiest way is:

//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));

output:
1.112.0
1.10.0
1.1.0

Solution 5:[5]

Here's a solution that will sort Semantic Versioning (https://semver.org). Nuget.Core offers a SemanticVersion class that operates very similarly to the standard Version class in .net. It will parse your string into an IComparable and IEquatable object so you can compare multiple versions or sort them within a collection, etc.

Nuget.Core: https://www.nuget.org/packages/nuget.core/ (you can pull this library via 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();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fredou
Solution 2 jdweng
Solution 3 Nam Tran
Solution 4 Dr.Net
Solution 5 Bill Tarbell