Hi, Is it possible to sort a string from a timer? I have this result on my Timer.Text. I just want a simple way to display the timer from fastest to slowest result.
string time1 = “01:14:00”;
string time2 = “02:06:00”;
string time3 = “04:09:00”;
string time4 = “11:12:01”;
Or do you have any better solution for this? Thanks in advance.
Why store them as strings at all? If you store them as time objects (hours, minutes, seconds), then you can just override ToString to output in the format you have here. That makes organization dead-simple, just implement iComparable and order it numerically.
DateTime objects I think are already set up to do this, but it’s usually overkill- making your own time object is usually faster than reading through the documentation for it too, honestly.
Anyways, I’m pretty sure this would work as-is even without any of that. Just put them all in a list and use Sort on the list and see if that works.
2 Likes
Can’t argue there, because surely the timer is already in some number
That said, for fun:
public static void Main(string[] args)
{
string [] times = new string[] {"01:14:00",
"02:06:00",
"04:09:00",
"11:12:01",
"02:06:02" };
Array.Sort(times, TimerSort);
foreach(string t in times) Console.WriteLine("Times : " + t);
}
static int TimerSort(string a, string b){
string [] _a = a.Split(':');
string [] _b = b.Split(':');
int i = 0, ret = 0;
for( ; i < _a.Length ; ++i){
ret = int.Parse(_a[i]).CompareTo(int.Parse(_b[i]));
if(ret != 0) return ret;
}
return ret;
}
Created on Rextester. Too lazy to fix the formatting/indentation. lol
1 Like
Out of curiosity, do you think that’s necessary? If the formatting is absolute (00:00:00) and in order of increasing precision (hour/minute/second), wouldn’t a parameterless list sort work for the strings themselves?
1 Like
DonLoquacious:
Why store them as strings at all? If you store them as time objects (hours, minutes, seconds), then you can just override ToString to output in the format you have here. That makes organization dead-simple, just implement iComparable and order it numerically.
DateTime objects I think are already set up to do this, but it’s usually overkill- making your own time object is usually faster than reading through the documentation for it too, honestly.
Anyways, I’m pretty sure this would work as-is even without any of that. Just put them all in a list and use Sort on the list and see if that works.
methos5k:
Can’t argue there, because surely the timer is already in some number
That said, for fun:
public static void Main(string[] args)
{
string [] times = new string[] {"01:14:00",
"02:06:00",
"04:09:00",
"11:12:01",
"02:06:02" };
Array.Sort(times, TimerSort);
foreach(string t in times) Console.WriteLine("Times : " + t);
}
static int TimerSort(string a, string b){
string [] _a = a.Split(':');
string [] _b = b.Split(':');
int i = 0, ret = 0;
for( ; i < _a.Length ; ++i){
ret = int.Parse(_a[i]).CompareTo(int.Parse(_b[i]));
if(ret != 0) return ret;
}
return ret;
}
Created on Rextester. Too lazy to fix the formatting/indentation. lol
wooh! thanks for fast reply… I’ll definitely try this one…
methos5k:
Can’t argue there, because surely the timer is already in some number
That said, for fun:
public static void Main(string[] args)
{
string [] times = new string[] {"01:14:00",
"02:06:00",
"04:09:00",
"11:12:01",
"02:06:02" };
Array.Sort(times, TimerSort);
foreach(string t in times) Console.WriteLine("Times : " + t);
}
static int TimerSort(string a, string b){
string [] _a = a.Split(':');
string [] _b = b.Split(':');
int i = 0, ret = 0;
for( ; i < _a.Length ; ++i){
ret = int.Parse(_a[i]).CompareTo(int.Parse(_b[i]));
if(ret != 0) return ret;
}
return ret;
}
Created on Rextester. Too lazy to fix the formatting/indentation. lol
You can write it like this with the same results (if all the strings share the same format)
public static void Main(string[] args)
{
string [] times = new string[] {"01:14:00", "02:06:00", "04:09:00", "11:12:01", "02:06:02" };
Array.Sort(times);
foreach(string t in times) Console.WriteLine("Times : " + t);
}
2 Likes
DonLoquacious:
Out of curiosity, do you think that’s necessary? If the formatting is absolute (00:00:00) and in order of increasing precision (hour/minute/second), wouldn’t a parameterless list sort work for the strings themselves?
I’m not good at coding, I only understand the basic stuff. By using string (if possible) to sort things out first come in my mind. lol
Err, my bad.
I often find the “long way” by mistake. =)
2 Likes
your post really helps me alot. Thank u
No problem. You should just sort it straight, as mentioned.
No worries, I was responding to methos5k and my question was genuine- I’ve never done an array/list sort on strings for numerical ordering, so I wasn’t actually certain if it would be that simple or if there was a reason for all of the parsing and such, lol.
1 Like
DonLoquacious:
No worries, I was responding to methos5k and my question was genuine- I’ve never done an array/list sort on strings for numerical ordering, so I wasn’t actually certain if it would be that simple or if there was a reason for all of the parsing and such, lol.
actually methos5k provided the code works and I haven’t found any issue yet as my development for a simple project still going on.
TimeSpan.Parse would be able to do the parsing of the strings into time units for you. Shortening the ‘TimerSort’ method down significantly:
1 Like
another great stuff. Much obliged…