IN sql we can pass two arguments to the ‘order by’ component, and it
will order the results by A, and then B in the cases where A is the
same.
Can anyone think of a way to do the same thing with a ruby array? For
example, if the array holds objects that have attributes/methods
“lastname” & “firstname”, to order the objects in a similar way to the
sql query?
Can anyone think of a way to do the same thing with a ruby array? For
example, if the array holds objects that have attributes/methods
“lastname” & “firstname”, to order the objects in a similar way to the
sql query?
IN sql we can pass two arguments to the ‘order by’ component, and it
will order the results by A, and then B in the cases where A is the
same.
Can anyone think of a way to do the same thing with a ruby array? For
example, if the array holds objects that have attributes/methods
“lastname” & “firstname”, to order the objects in a similar way to the
sql query?
Here’s one way: the trick is to create an array with the fields you want
to order by in the sort_by block:
Here’s one way: the trick is to create an array with the fields you want
to order by in the sort_by block:
Hope this helps,
Jesus.
That’s a neat trick, thanks! I actually like the first one better
though as you can swap a & b around in each test to get (eg) sorted by
name ascending and then a date field descending.
That’s a neat trick, thanks! I actually like the first one better
though as you can swap a & b around in each test to get (eg) sorted by
name ascending and then a date field descending.
This is often true as sort_by only calls methods on each object once,
but it
is not universally true – see http://ruby-doc.org/core/classes/Enumerable.html#M003151 for more
information, and run some benchmarks for your use case if it’s a big
issue.
That’s a neat trick, thanks! I actually like the first one better
though as you can swap a & b around in each test to get (eg) sorted by
name ascending and then a date field descending.
Just use -x.b:
a.sort_by{|x| [x.name, -x.date]}
Doesn’t always work. I keep this handy:
class RevCmp
attr_reader :this
def initialize(obj)
@this = obj
end
def <=>(other)
other.this <=> @this
end
# not delegating anything else because this is explicitly a
throwaway
# object used only inside a sort_by block
end
and then you have
a.sort_by {|x| [x.name, RevCmp.new(x.date)]}
you could even use a top-level method so you can say
a.sort_by {|x| [x.name, descending(x.date)]}
where descending(x) returns RevCmp.new(x)
You could even mix it into object to get
a.sort_by {|x| [x.name, x.date.descending]}
where I use the underscores as a cosmetic way of making it stand out
inside the sort block
In theory this is less efficient since there are two Array instances
created per comparison. If you go for the Array solution, using #sort_by is probably better:
Why does this kind of sorting work on an Array input?
How would I know that it first starts by the first
Array member, and then the second? Why is it not
the other way (from last to first, rather from first
to last entry)?
Why does this kind of sorting work on an Array input?
When an Array calls a method, the block specified after
the method call does not have to take an array as
an argument. Here is an example:
class Array
def do_stuff
if block_given?
map do |array_elmt|
yield array_elmt
end
else
self
end
end
end
data = [10, 20, 30]
y = data.do_stuff { |x| x*2 }
p y
y = data.do_stuff
p y
–output:–
[20, 40, 60]
[10, 20, 30]
Enumerable#sort_by() uses a block in a similar fashion.
sort_by() sends each
element of an Array that you want to sort to the block to create a new
value that it will use
as a stand in for the original value when sorting. The return values
from the block are what sort_by() uses as the values to sort. For
example, look at this Hash:
mapped_orig_vals = {
['David', 2] => objA
['David', 1] => objB,
}
After creating each entry in the hash, sort_by() sorts the keys, which
are arrays, to produce:
ordered_keys = [
[‘David’, 1],
[‘David’, 2],
]
After sorting the keys, sort_by() looks up the value
associated with each key to get the original value, e.g.
ordered_orig_vals = ordered_keys.map do |key|
mapped_orig_values[key]
end
#=>[objB, objA]
How would I know that it first starts by the first
Array member, and then the second?
If by Array you mean the Array returned by the sort_by() block, you
would know by reading about how ruby determines whether two
arrays are equal or not:
In every computer programming language I’ve studied, things like Arrays
and Strings are always compared by starting with the first
element/character, and if they are the same, then the second element is
compared, etc. Once a difference is found, then one Array/String is
considered smaller than the other Array/String. If all the elements are
the same but one Array/String is shorter than the other, the shorter one
is considered smaller. If all the elements are the same, and they are
the same length, then they are considered equal.
On the other hand, if by Array you mean the original Array you are
trying to sort, it is irrelevant which end of the array you start at.