Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7047405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:46:02+00:00 2026-05-28T02:46:02+00:00

I have a list of strings. Each string follows the pattern {Path}\UpdateTo{Version}-{Order}. I need

  • 0

I have a list of strings. Each string follows the pattern “{Path}\UpdateTo{Version}-{Order}”.

I need to sort the list such that the lowest version numbers are at the top. In the case when there is multiple files with the same version number then an optional order parameter is appended. If an order is present on any of the strings then it should appear above the strings with the same version number that do not have an order number.

For example, give the following list (note the items are randomly ordered):

var files = new List<string>() {
    @"C:\Migrations\UpdateTo1.2-2",
    @"C:\Migrations\UpdateTo1.5-2",
    @"C:\Migrations\UpdateTo1.2",
    @"C:\Migrations\UpdateTo1.4",
    @"C:\Migrations\UpdateTo1.1",
    @"C:\Migrations\UpdateTo1.5",
    @"C:\Migrations\UpdateTo1.2-1",
    @"C:\Migrations\UpdateTo1.5-1"
};

The result would be:

var files = new List<string>() {
    @"C:\Migrations\UpdateTo1.1",
    @"C:\Migrations\UpdateTo1.2-1",
    @"C:\Migrations\UpdateTo1.2-2",
    @"C:\Migrations\UpdateTo1.2",
    @"C:\Migrations\UpdateTo1.4",
    @"C:\Migrations\UpdateTo1.5-1",
    @"C:\Migrations\UpdateTo1.5-2",
    @"C:\Migrations\UpdateTo1.5"
}

I’ve been trying with all sorts of ideas but so far my attempts have been a complete mess. I’d appreciate it if someone could help. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:46:03+00:00Added an answer on May 28, 2026 at 2:46 am

    I used a temporary class to handle the parsing and the comparisons to get the desired output. I’ve included code that gets everything back to how you requested it but the “temporary” class introduced may have more value to you over just the paths (?).

    Usage:

    var sorted = files.Select(f => new UpdateTo(f))
        .OrderBy(u => u)
        .Select(u => u.Path)
        .ToArray();
    

    The code:

    class UpdateTo : IComparable<UpdateTo>
    {
        public decimal Version { get; private set; }
        public int Order { get; private set; }
        public string Path { get; private set; }
    
        private const string Prefix = "UpdateTo";
    
        public UpdateTo(string path)
        {
            /* No error-checking here -- BEWARE!! */
            Path = path;
    
            string toParse = Path.Substring(Path.IndexOf(Prefix, StringComparison.InvariantCultureIgnoreCase) + Prefix.Length);
            var split = toParse.Split('-');
    
            Version = decimal.Parse(split[0]);
            Order = split.Length == 2 ? int.Parse(split[1]) : int.MaxValue;
        }
    
        public int CompareTo(UpdateTo other)
        {
            int versionCompare = Version.CompareTo(other.Version);
            return versionCompare == 0 ? Order.CompareTo(other.Order) : versionCompare;
        }
    }
    

    And the test…

    [Test]
    public void ListSort()
    {
        const string first = @"C:\Migrations\UpdateTo1.1";
        const string second = @"C:\Migrations\UpdateTo1.2-1";
        const string third = @"C:\Migrations\UpdateTo1.2-2";
        const string fourth = @"C:\Migrations\UpdateTo1.2";
        const string fifth = @"C:\Migrations\UpdateTo1.4";
        const string sixth = @"C:\Migrations\UpdateTo1.5-1";
        const string seventh = @"C:\Migrations\UpdateTo1.5-2";
        const string eighth = @"C:\Migrations\UpdateTo1.5";
    
        var files = new List<string>{third, seventh, fourth, fifth, first, eighth, second, sixth};
    
        var sorted = files.Select(f => new UpdateTo(f))
            .OrderBy(u => u)
            .Select(u => u.Path)
            .ToArray();
    
        Assert.AreEqual(first, sorted[0]);
        Assert.AreEqual(second, sorted[1]);
        Assert.AreEqual(third, sorted[2]);
        Assert.AreEqual(fourth, sorted[3]);
        Assert.AreEqual(fifth, sorted[4]);
        Assert.AreEqual(sixth, sorted[5]);
        Assert.AreEqual(seventh, sorted[6]);
        Assert.AreEqual(eighth, sorted[7]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel spreadsheet containing a list of strings. Each string is made
I have a list containing version strings, such as things: versions_list = [1.1.2, 1.0.0,
Suppose I have a list of strings where each string is exactly 4 characters
I have a List of strings that is regenerated every 5 seconds. I want
I have sorted list of strings that I move between php and java. to
Using bash, I have a list of strings that I want to use to
Kind of a weird question, but. I need to have a list of strings
I have a list of 400 strings that all end in _GONOGO or _ALLOC.
I have created a Map as follows: private Map<String, List<Client>> clientCatalogue; this.clientCatalogue = new
I have a class that follows the form: public class Cat { public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.