Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes a comparison function have to be symmetric? (or: Sorting a list with Don't care values)
    text
    copied!<p>I have a list of objects with an enumeration attribute that I wish to sort by. Of the enumeration values, I only care about A, B, and C, and require that in the sorted list, A &lt; B &lt; C. Of values D, E, ..., their order doesn't matter and it doesn't matter if they are interspersed in the list.</p> <p>When implementing the comparison, is it necessary to specify a consistent ordering for the extraneous enumeration values relative to the critical values? As an example, does it cause issues if a comparison between A,D results in -1 while the comparison between D,A results in 0, or also -1?</p> <p>This code is being implemented in C#, which I believe uses quicksort. After reviewing the quicksort algorithm, it looks like this may be an issue if one of the extraneous values becomes the pivot.</p> <p>Implementation-wise - what's the best way to sort something like this? Having to explicitly handle the extraneous values results in a bunch more cases to address. I could probably assign values to the enumerations but there's no guarantee that there wouldn't be a different sorting order that would be useful in other cases. I'm not sure if it's sufficient to simply assume that the list will be stripped of extra values before it's sorted, since this is likely to cause bugs that are hard to find if the assumption is violated. I could throw ArgumentException when those values are used, but then I'd be adding enough cases that I might as well just sort them instead. About the best option I can come up with is to create a casting function to transform the enums into ints.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload