Search code examples
c#arrayslistsortingversion-sort

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

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