Sorting a list of IComparables

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I have a DateTime class (my own, not System.DateTime) which implements IComparable.
Here is a method that takes a List<DateTime> and sorts it:
private List<DateTime> SortDateTimes(List<DateTime> dateTimes)
for (int i = 0; i < dateTimes.Count; i++)
for (int j = 0; j < dateTimes.Count; j++)
if (dateTimes[i] < dateTimes[j])
var temp = dateTimes[i];
dateTimes[i] = dateTimes[j];
dateTimes[j] = temp;
return dateTimes;
I believe using foreach loops instead of for is suitable here, but having the following variable names drives me crazy. :D
foreach(var dateTime1 in dateTimes)
{
foreach(var dateTime2 in datetimes)
...
What are your thoughts?
Thanks!
c# sorting
 |Â
show 2 more comments
up vote
1
down vote
favorite
I have a DateTime class (my own, not System.DateTime) which implements IComparable.
Here is a method that takes a List<DateTime> and sorts it:
private List<DateTime> SortDateTimes(List<DateTime> dateTimes)
for (int i = 0; i < dateTimes.Count; i++)
for (int j = 0; j < dateTimes.Count; j++)
if (dateTimes[i] < dateTimes[j])
var temp = dateTimes[i];
dateTimes[i] = dateTimes[j];
dateTimes[j] = temp;
return dateTimes;
I believe using foreach loops instead of for is suitable here, but having the following variable names drives me crazy. :D
foreach(var dateTime1 in dateTimes)
{
foreach(var dateTime2 in datetimes)
...
What are your thoughts?
Thanks!
c# sorting
6
This doesn't have a 'reinventing the wheel tag'...: have you considered usingList.Sort? Here you have provided a something not far off a (comparatively poor, since it lacks a cutoff) bubble sort, which will taken^2steps to sort your list, wherenis the number of items in the list. An 'efficient' sorting algorithm (asList.Sortwill use) will do the same job inn log(n)steps, which is big difference for a largen.
â VisualMelon
Feb 5 at 18:24
Hmm, as said above: stackoverflow.com/questions/26868600/â¦
â ÃÂìýÃÂñ á¿¥Ã栨Â
Feb 5 at 18:25
@VisualMelon so all I need to do isdateTimes.Sort()?
â Sipo
Feb 5 at 18:27
@Sipo yeah, I think so. (It's a bit of a weird method, because it looks at the type (in runtime) to work out if it can sort it, part of which involves checking if it isIComparable; there are ways to make it more 'concrete')
â VisualMelon
Feb 5 at 18:28
3
I'm not sure it would qualify as an answer (since it isn't really about your code)... just have a quick read of the Remarks forList<T>.Sortwhich details how theDefaultComparer<T>is prepared. As long as you don't try to do anything silly with it, nothing will go wrong, and if you do try to do something silly with it then it will tell you by throwing an exception.
â VisualMelon
Feb 5 at 18:37
 |Â
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a DateTime class (my own, not System.DateTime) which implements IComparable.
Here is a method that takes a List<DateTime> and sorts it:
private List<DateTime> SortDateTimes(List<DateTime> dateTimes)
for (int i = 0; i < dateTimes.Count; i++)
for (int j = 0; j < dateTimes.Count; j++)
if (dateTimes[i] < dateTimes[j])
var temp = dateTimes[i];
dateTimes[i] = dateTimes[j];
dateTimes[j] = temp;
return dateTimes;
I believe using foreach loops instead of for is suitable here, but having the following variable names drives me crazy. :D
foreach(var dateTime1 in dateTimes)
{
foreach(var dateTime2 in datetimes)
...
What are your thoughts?
Thanks!
c# sorting
I have a DateTime class (my own, not System.DateTime) which implements IComparable.
Here is a method that takes a List<DateTime> and sorts it:
private List<DateTime> SortDateTimes(List<DateTime> dateTimes)
for (int i = 0; i < dateTimes.Count; i++)
for (int j = 0; j < dateTimes.Count; j++)
if (dateTimes[i] < dateTimes[j])
var temp = dateTimes[i];
dateTimes[i] = dateTimes[j];
dateTimes[j] = temp;
return dateTimes;
I believe using foreach loops instead of for is suitable here, but having the following variable names drives me crazy. :D
foreach(var dateTime1 in dateTimes)
{
foreach(var dateTime2 in datetimes)
...
What are your thoughts?
Thanks!
c# sorting
edited Feb 5 at 18:26
asked Feb 5 at 18:13
Sipo
1414
1414
6
This doesn't have a 'reinventing the wheel tag'...: have you considered usingList.Sort? Here you have provided a something not far off a (comparatively poor, since it lacks a cutoff) bubble sort, which will taken^2steps to sort your list, wherenis the number of items in the list. An 'efficient' sorting algorithm (asList.Sortwill use) will do the same job inn log(n)steps, which is big difference for a largen.
â VisualMelon
Feb 5 at 18:24
Hmm, as said above: stackoverflow.com/questions/26868600/â¦
â ÃÂìýÃÂñ á¿¥Ã栨Â
Feb 5 at 18:25
@VisualMelon so all I need to do isdateTimes.Sort()?
â Sipo
Feb 5 at 18:27
@Sipo yeah, I think so. (It's a bit of a weird method, because it looks at the type (in runtime) to work out if it can sort it, part of which involves checking if it isIComparable; there are ways to make it more 'concrete')
â VisualMelon
Feb 5 at 18:28
3
I'm not sure it would qualify as an answer (since it isn't really about your code)... just have a quick read of the Remarks forList<T>.Sortwhich details how theDefaultComparer<T>is prepared. As long as you don't try to do anything silly with it, nothing will go wrong, and if you do try to do something silly with it then it will tell you by throwing an exception.
â VisualMelon
Feb 5 at 18:37
 |Â
show 2 more comments
6
This doesn't have a 'reinventing the wheel tag'...: have you considered usingList.Sort? Here you have provided a something not far off a (comparatively poor, since it lacks a cutoff) bubble sort, which will taken^2steps to sort your list, wherenis the number of items in the list. An 'efficient' sorting algorithm (asList.Sortwill use) will do the same job inn log(n)steps, which is big difference for a largen.
â VisualMelon
Feb 5 at 18:24
Hmm, as said above: stackoverflow.com/questions/26868600/â¦
â ÃÂìýÃÂñ á¿¥Ã栨Â
Feb 5 at 18:25
@VisualMelon so all I need to do isdateTimes.Sort()?
â Sipo
Feb 5 at 18:27
@Sipo yeah, I think so. (It's a bit of a weird method, because it looks at the type (in runtime) to work out if it can sort it, part of which involves checking if it isIComparable; there are ways to make it more 'concrete')
â VisualMelon
Feb 5 at 18:28
3
I'm not sure it would qualify as an answer (since it isn't really about your code)... just have a quick read of the Remarks forList<T>.Sortwhich details how theDefaultComparer<T>is prepared. As long as you don't try to do anything silly with it, nothing will go wrong, and if you do try to do something silly with it then it will tell you by throwing an exception.
â VisualMelon
Feb 5 at 18:37
6
6
This doesn't have a 'reinventing the wheel tag'...: have you considered using
List.Sort? Here you have provided a something not far off a (comparatively poor, since it lacks a cutoff) bubble sort, which will take n^2 steps to sort your list, where n is the number of items in the list. An 'efficient' sorting algorithm (as List.Sort will use) will do the same job in n log(n) steps, which is big difference for a large n.â VisualMelon
Feb 5 at 18:24
This doesn't have a 'reinventing the wheel tag'...: have you considered using
List.Sort? Here you have provided a something not far off a (comparatively poor, since it lacks a cutoff) bubble sort, which will take n^2 steps to sort your list, where n is the number of items in the list. An 'efficient' sorting algorithm (as List.Sort will use) will do the same job in n log(n) steps, which is big difference for a large n.â VisualMelon
Feb 5 at 18:24
Hmm, as said above: stackoverflow.com/questions/26868600/â¦
â ÃÂìýÃÂñ á¿¥Ã栨Â
Feb 5 at 18:25
Hmm, as said above: stackoverflow.com/questions/26868600/â¦
â ÃÂìýÃÂñ á¿¥Ã栨Â
Feb 5 at 18:25
@VisualMelon so all I need to do is
dateTimes.Sort()?â Sipo
Feb 5 at 18:27
@VisualMelon so all I need to do is
dateTimes.Sort()?â Sipo
Feb 5 at 18:27
@Sipo yeah, I think so. (It's a bit of a weird method, because it looks at the type (in runtime) to work out if it can sort it, part of which involves checking if it is
IComparable; there are ways to make it more 'concrete')â VisualMelon
Feb 5 at 18:28
@Sipo yeah, I think so. (It's a bit of a weird method, because it looks at the type (in runtime) to work out if it can sort it, part of which involves checking if it is
IComparable; there are ways to make it more 'concrete')â VisualMelon
Feb 5 at 18:28
3
3
I'm not sure it would qualify as an answer (since it isn't really about your code)... just have a quick read of the Remarks for
List<T>.Sort which details how the DefaultComparer<T> is prepared. As long as you don't try to do anything silly with it, nothing will go wrong, and if you do try to do something silly with it then it will tell you by throwing an exception.â VisualMelon
Feb 5 at 18:37
I'm not sure it would qualify as an answer (since it isn't really about your code)... just have a quick read of the Remarks for
List<T>.Sort which details how the DefaultComparer<T> is prepared. As long as you don't try to do anything silly with it, nothing will go wrong, and if you do try to do something silly with it then it will tell you by throwing an exception.â VisualMelon
Feb 5 at 18:37
 |Â
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186858%2fsorting-a-list-of-icomparables%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
6
This doesn't have a 'reinventing the wheel tag'...: have you considered using
List.Sort? Here you have provided a something not far off a (comparatively poor, since it lacks a cutoff) bubble sort, which will taken^2steps to sort your list, wherenis the number of items in the list. An 'efficient' sorting algorithm (asList.Sortwill use) will do the same job inn log(n)steps, which is big difference for a largen.â VisualMelon
Feb 5 at 18:24
Hmm, as said above: stackoverflow.com/questions/26868600/â¦
â ÃÂìýÃÂñ á¿¥Ã栨Â
Feb 5 at 18:25
@VisualMelon so all I need to do is
dateTimes.Sort()?â Sipo
Feb 5 at 18:27
@Sipo yeah, I think so. (It's a bit of a weird method, because it looks at the type (in runtime) to work out if it can sort it, part of which involves checking if it is
IComparable; there are ways to make it more 'concrete')â VisualMelon
Feb 5 at 18:28
3
I'm not sure it would qualify as an answer (since it isn't really about your code)... just have a quick read of the Remarks for
List<T>.Sortwhich details how theDefaultComparer<T>is prepared. As long as you don't try to do anything silly with it, nothing will go wrong, and if you do try to do something silly with it then it will tell you by throwing an exception.â VisualMelon
Feb 5 at 18:37