The block receives two parameters for you to specify how they should be compared.
Ruby array sort.
A negative index is assumed relative to the end of the array that is an index of 1 indicates the last element of the array 2 is the next to last element in the array and so on.
Sort with blocks sort in descending order and sort in place.
The second form creates a copy of the array passed as a parameter the array is generated by calling to ary on the parameter.
An array of sorted elements.
Returns a new array.
Let us see an example.
It s also possible to sort in place using the sort.
This means that the original array will change instead of creating a new one which can be good for performance.
Ruby arrays can hold objects such as string integer fixnum hash symbol even other array objects.
Arrays let you represent lists of data in your programs.
Once you have data in an array you can sort it remove duplicates reverse its order extract sections of the array or search through arrays for specific data.
When a size and an optional default are sent an array is created with size copies of default take notice that all elements will reference the same object default.
You can also pass it an optional block if you want to do some custom sorting.
Ruby arrays are not as rigid as arrays in other languages.
Ruby sort arrays use the sort method.
You can also convert an array to a string transform one array of data into another and roll up an array into a single value.