Optional parameters: Difference between revisions
Content added Content deleted
m (→{{header|Sidef}}: modified the code to work with Sidef 2.30) |
|||
Line 1,898: | Line 1,898: | ||
]; |
]; |
||
say table_sort(table, column: 1) |
say table_sort(table, column: 1);</lang> |
||
{{out}} |
{{out}} |
||
<pre>[["Ottowa", "Canada"], ["Mexico City", "Mexico"], ["Washington", "USA"]]</pre> |
<pre>[["Ottowa", "Canada"], ["Mexico City", "Mexico"], ["Washington", "USA"]]</pre> |
||
Missing the point, we can also create and provide a custom method for sorting to ''ordering'': |
|||
<lang ruby>class String { |
<lang ruby>class String { |
||
method my_sort(arg) { |
method my_sort(arg) { |
||
(self.len <=> arg.len) |
(self.len <=> arg.len) -> |
||
|| (self.lc <=> arg.lc) |
|| (self.lc <=> arg.lc) -> |
||
|| (self <=> arg) |
|| (self <=> arg) |
||
} |
} |
||
} |
} |
||
say table_sort(table, column: 1, ordering: 'my_sort') |
say table_sort(table, column: 1, ordering: 'my_sort');</lang> |
||
{{out}} |
{{out}} |
||
<pre>[["Washington", "USA"], ["Ottowa", "Canada"], ["Mexico City", "Mexico"]]</pre> |
<pre>[["Washington", "USA"], ["Ottowa", "Canada"], ["Mexico City", "Mexico"]]</pre> |