cs12dotnet8 icon indicating copy to clipboard operation
cs12dotnet8 copied to clipboard

Pages 316 and 317 - Sorting

Open rmantel23 opened this issue 1 year ago • 4 comments

You already have an entry for this in the errata, but I don't think that is correct. The branch you marked as not being able to be reached can be reached if there are multiple null names, see example below:

image

image

I think the below branch can never be reached though:

image

The above can be replaced by:

image

rmantel23 avatar Jun 14 '24 22:06 rmantel23

I've moved the big comment and added a couple of new comments to clarify the code. The final else still needs to return 0 though to indicate that when both objects are null, they are equal.

markjprice avatar Jun 15 '24 07:06 markjprice

That final else will never be reached though the way you have it set up. If other is not null it goes into the first if. Then you have an 'else if (other is null)', but we already know it is null since it didn't go into the 1st if, and it will always go into the 2nd and never reach the final else. So, you can just make the 2nd an else instead and set position to -1 there.

rmantel23 avatar Jun 17 '24 20:06 rmantel23

Instead of writing the final else (which will not be ever executed), and also to avoid the compilation error, you can initialize the int position with 0 (starting with the assumption that "this and other are at the same position". It`s less confusing than reading an else which will never be executed. In this way, you can even remove the last else from the first if branch.

Besides this, in the book, at page 317 at the bottom, when you display the list, Simon is first, than null Person is the second. But back on page 315, when you initialized the people object at the bottom of the page, the first element is null and the second one is Simon. So, the correct display from page 317 should be: null Person and then Simon etc.

vladmeici avatar Nov 15 '24 10:11 vladmeici

I will revisit this whole example for the .NET 10 edition.

markjprice avatar Nov 16 '24 12:11 markjprice