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
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:
The code:
And the test…