I have an example string:
"60 seconds,1;120 seconds,2;180 seconds,3;15 seconds,15;20 seconds,20;30 seconds,30"
I'd like to sort it by the first number only. So after sorting, it'll look like:
"15 seconds,15;20 seconds,20;30 seconds,30;60 seconds,1;120 seconds,2;180 seconds,3"
Some limitations: I can't hard code anything. I've created example code to demonstrate my solution so far. Basically, I create 2 string arrays and 1 int array. The first string array takes the string and splits it by the semicolon. The second string array is a middle man array. The int array is the middle man array converted to int. I use the int array as a reference to sort my first string array using Array.sort Here is my example code:
using System;
using System.Linq;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string myExampleString = "60 seconds,1;120 seconds,2;180 seconds,3;15 seconds,15;20 seconds,20;30 seconds,30";
string mySortedString = "";
// Count the number of semicolons and + 1 to get arraySize
int arraySize = myExampleString.Count(f => (f == ';')) + 1;
// To hold our original string, split by the ';'
String[] stringArray = new String[arraySize];
// Our middle man array to make sorting easier
String[] organizedStringArray = new String[arraySize];
// Our middle man array converted to int
int[] intArray = new int[arraySize];
// Create the stringArray without the semicolons
stringArray = myExampleString.Split(';');
for(int i=0; i<arraySize; i++)
{
// Our stringArray elements:
/*
60 seconds,1
120 seconds,2
180 seconds,3
15 seconds,15
20 seconds,20
30 seconds,30
*/
// Let's make our organizedStringArray elements:
/*
60
120
180
15
20
30
*/
Regex regex = new Regex(" .*");
organizedStringArray[i] = regex.Replace(stringArray[i], "");
}
// Convert the middleman array to ints
intArray = Array.ConvertAll(organizedStringArray, s => int.Parse(s));
// Use the sort function to sort our orignal stringArray, using intArray as the reference
Array.Sort(intArray, stringArray);
// Append the results into mySortedString
for(int i=0; i<arraySize; i++)
{
if(i < arraySize - 1 )
{
mySortedString += stringArray[i] + ";";
}
else
{
mySortedString += stringArray[i];
}
}
Console.WriteLine(mySortedString);
}
}