Customizing the Ruby Sort Method to Force an Element to the End of the Sorted Array

0
780

This guide walks through how to alter the built in Ruby sort method so that it forces a specific element to be placed at the end of a sorted array.

Summary

Alter the built in sort method in Ruby so that it always places a specific element at the end of a sorted array.

Exercise File

Code File

Exercise Description

Given the alphabet in an array based form:

['a', 'b', 'c', ..., 'z']

Customize the sort method so that it places the letter k at the end of the array:

Expected Output

["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "k"]

Real World Usage

In order to build this solution, you will need to understand a number of important concepts, such as:

  • How to pass a block to the sort method
  • Working with the spaceship operator for comparisons

Solution

Can be found on the solutions branch on github.

LEAVE A REPLY

Please enter your comment!
Please enter your name here