I am trying to get a custom sort working. Eventually I will try to apply this to a database query but before that I want to prove the logic works in an in-memory sort. The problem is, I cannot get even the most simplest of sort methods to work!
Sort method:
def self.sort!(reputation_alerts)
#reputation_alerts.sort { |a, b| b <=> a }
reputation_alerts.sort { |a, b| b.id <=> a.id }
end
I had hoped the first sort (currently commented out) would return reputation_alerts in reverse order. It did nothing.
I had hoped the second sort would return reputation alerts sorted by id in reverse order. It did nothing.
I am testing the sort by iterating through the returned reputation_alerts object and dumping the contents of each one; every time the "sorted" results are the same as the original order...
Test method:
test "ReputationAlertSort PassedArray SortsArrayCorrectly" do
@customer = customers(:one)
@customer.update(keywords: { 1 => "bob", 2 => "wibble", 3 => "random" } )
@customer.get_keywords
@customer.reputation_alerts.clear
@customer.reputation_alerts.build(
id: 2, rep_keyword: "random", search_engine: "bing", rank: 1)
@customer.reputation_alerts.build(
id: 1, rep_keyword: "wibble", search_engine: "google", rank: 2)
@customer.reputation_alerts.build(
id: 3, rep_keyword: "wibble", search_engine: "google", rank: 1)
@customer.reputation_alerts.build(
id: 4, rep_keyword: "wibble", search_engine: "yahoo", rank: 2)
@customer.reputation_alerts.build(
id: 5, rep_keyword: "wibble", search_engine: "yahoo", rank: 1)
@customer.reputation_alerts.build(
id: 6, rep_keyword: "bob", search_engine: "google", rank: 2)
@customer.reputation_alerts.build(
id: 7, rep_keyword: "bob", search_engine: "yahoo", rank: 1)
@customer.reputation_alerts.build(
id: 8, rep_keyword: "random", search_engine: "bing", rank: 2)
ReputationAlert.sort!(@customer.reputation_alerts)
assert_sorted(1, 7)
assert_sorted(2, 6)
assert_sorted(3, 5)
assert_sorted(4, 4)
assert_sorted(5, 3)
assert_sorted(6, 2)
assert_sorted(7, 1)
assert_sorted(8, 8)
end
def assert_sorted(post_sort_position, pre_sort_position)
assert_equal post_sort_position,
@customer.reputation_alerts[pre_sort_position - 1].id, dump
end
def dump
dump = ""
@customer.reputation_alerts.each do |a|
dump += "\n#{a.id}, #{a.rep_keyword}, #{a.search_engine}, #{a.rank}"
end
dump
end
Any suggestions? Once I can get this working I can move on to the actual (complex) sort...
EDIT:
I thought the type of object I was trying to sort might be posing a problem, so I tried an even more basic sort:
temp.sort
:
test "ReputationAlertSort PassedArray SortsArrayCorrectly" do
temp = 1..7
temp.sort { |a, b| b <=> a }
raise "#{temp}"
end
This returns temp still unsorted. (Kind of expected due to the lack of a !)
temp.sort!
test "ReputationAlertSort PassedArray SortsArrayCorrectly" do
temp = 1..7
temp.sort! { |a, b| b <=> a }
raise "#{temp}"
end
This gives the error: NoMethodError: undefined method `sort!' for 1..7:Range
temp = temp.sort!
test "ReputationAlertSort PassedArray SortsArrayCorrectly" do
temp = 1..7
temp = temp.sort { |a, b| b <=> a }
raise "#{temp}"
end
This DOES work, at least, so maybe I can use that as a starting point to figure this out...