0

What does .sort do?

a=[“b”, “c”, “a”, “d”] a.sort puts a[1] #answer is “a”. How?

1st Jan 2018, 5:32 PM
Arfaan
Arfaan - avatar
1 Antwort
+ 7
Actually, it should print out: c .sort does what them name suggests, it sorts the list. However, a regular .sort returns just the sorted representation of the list in question - it doesn't *change* it inside. So that you can assign a new variable to it, like: b = a.sort In order to change a itself, you either reassign it, like: a = a.sort or use a shorthand with an exclamation mark at the end: a.sort! The last command shifts the list's elements inside and sorts them in the ascending order (by default).
1st Jan 2018, 5:58 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar