Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pages 316 and 317 - Sorting #52

Open
rmantel23 opened this issue Jun 14, 2024 · 4 comments
Open

Pages 316 and 317 - Sorting #52

rmantel23 opened this issue Jun 14, 2024 · 4 comments

Comments

@rmantel23
Copy link

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

@markjprice
Copy link
Owner

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.

@rmantel23
Copy link
Author

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.

@vladmeici
Copy link

vladmeici commented Nov 15, 2024

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.

@markjprice
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants