Ruby: sort by two array elements
✓ Published0🌍 Public
Aafbroman
Last edited Aug 20, 2012
Created on Aug 20, 2012
This example demonstrates a custom sorting routine for an array of sub-arrays, comparing elements by two keys in sequence. It first compares the values at index 1, converting them to lowercase with `downcase` to ensure case-insensitive ordering. If those values are equal, it falls back to comparing the values at index 2, also lowercased, using Ruby’s spaceship operator `<=>`. The sort is performed in-place via the `Array#sort` method with a block, making the secondary comparison a tie-breaker.
AI-generated description