Update
Changes after the revised spec ("please sort it"). I now convert the original line into a (double, int) tuple and work with that. That allows things to be sorted.
In the end, I format things the way you do originally. As @ralf says, you should consider having something that not just one weird string.
First create your list of numbers (by the way, this code should be in your question):
private static readonly List<string> annotatedNumbers = new List<string>
{
"6557.94172139931[6]",
"5354.8168281469[9]",
"5354.81740416957[9]",
"5760.29235605679[8]",
"6212.12135703147[7]",
"4869.7862090936[95]",
"4869.78639031275[95]",
"3681.97068695995[10]",
"3681.9727736492[10]",
};
Then write some code like this. It accumulates the indexes to remove and then, in a second pass, removes those indexes:
public static IEnumerable<(double distance, int layer)> GetTrimmedList(List<string> withDups)
{
var layerNumbersToCheck = new List<int>();
var distanceAndLayerNames = new List<(double distance, int layer)>();
foreach (var item in withDups)
{
var parts = item.Split('[', ']');
if (parts.Length < 2)
{
throw new ArgumentException($"Item: {item} incorrectly formatted");
}
if (!double.TryParse(parts[0], out var distance))
{
throw new ArgumentException($"Distance in item: {item} is not a valid double");
}
if (!int.TryParse(parts[1], out var layerNum))
{
throw new ArgumentException($"Layer number in item: {item} is not a number");
}
if (!layerNumbersToCheck.Contains(layerNum))
{
layerNumbersToCheck.Add(layerNum);
distanceAndLayerNames.Add((distance, layerNum));
}
}
var result = distanceAndLayerNames.OrderBy(item => item.layer);
return result;
}
}
Finally, some code to exercise the function:
public static void Test()
{
var result = GetTrimmedList(annotatedNumbers);
foreach(var item in result)
{
Console.WriteLine($"{item.distance}[{item.layer}]");
}
}
The result looks like:
6557.94172139931[6]
6212.12135703147[7]
5760.29235605679[8]
5354.8168281469[9]
3681.97068695995[10]
4869.7862090936[95]