Understanding descending sort of String based array


#1


https://www.codecademy.com/courses/ruby-beginner-en-ET4bU/3/4?curriculum_id=5059f8619189a5000201fbcb#


This code works, I just don't have a strong understanding on why it's working.


# This code sorts books in descending order, in-place below...
# This is code I wrote, which works, what effect does the ! have after the sort method name?
books.sort! do |firstBook, secondBook| 
   if firstBook < secondBook
      1 
   elsif firstBook > secondBook
     -1
   else
     0        
   end
end 

# This below code is the example that codeacademy provided for the sort, why in their example does it NOT have
# the ! after the books.sort?
#
    books.sort do |first, second| 
      if first < second # first book before second alphabetically
       -1
      elsif first > second # first after second
       1
      else # first and second are the same
       0
      end
    end


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.