I have a list of folder names that represent chapters, subchapters, sections, paragraphs and lines in a specification. A small sample of these folders looks like the following.
- 1_1_1
- 1_1_12
- 1_1_2
- 1_2_1
- 1_2_1_3_1
- 1_2_2
I need to write a function that sorts these numerically and taking account for hierarchical nesting. For instance the correct output of sorting the above would be.
- 1_1_1
- 1_1_2
- 1_1_12
- 1_2_1
- 1_2_1_3_1
- 1_2_2
Since this is very much the same way version numbers are sorted I have tried the following code which worked until it attempts to process an input with more than 4 sections (i.e. 1_2_1_3_1)
private List<Resource> OrderResources(List<Resource> list)
{
return list.OrderBy(v => System.Version.Parse(v.Id.Replace('_', '.'))).ToList();
}
The error I get is
System.ArgumentException : Version string portion was too short or too long. (Parameter 'input')