Sorting a list of IComparables

The name of the pictureThe name of the pictureThe name of the pictureClash 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!







share|improve this question

















  • 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











  • 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>.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

















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!







share|improve this question

















  • 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











  • 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>.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













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!







share|improve this question













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!









share|improve this question












share|improve this question




share|improve this question








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 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










  • @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>.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













  • 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











  • 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>.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








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
















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation