techdreams
techdreams

Reputation: 5585

Sort json data alphabetically in ruby on rails

I have this data with me:

[{:id=>250,
      :application_date=>"02/04/2016",
      :customer_number=>"",
      :customer_name=>"Neymar Silva Junior",
      :city=>"Auckland",
      :region=>"Auckland",
      :service=>"Electricity",
      :name=>"Bosco and Sons",
      :service_plan=>"Electricity Plan",
      :connection_type=>nil,
      :billing_method=>nil,
      :icp_number=>nil,
      :moving_date=>"",
      :supplier_commission=>21.0,
      :show_url=>"/applications/250"},
 {:id=>257,
      :application_date=>"27/05/2016",
      :customer_number=>"",
      :customer_name=>"Ariel name Parra",
      :city=>"Dunedin",
      :region=>"Dunedin",
      :service=>"Electricity",
      :name=>"Bosco and Sons",
      :service_plan=>"Electricity Plan",
      :connection_type=>nil,
      :billing_method=>nil,
      :icp_number=>nil,
      :moving_date=>"28/05/2016",
      :supplier_commission=>21.0,
      :show_url=>"/applications/257"},
 {:id=>291,
      :application_date=>"29/04/2016",
      :customer_number=>"aaaa",
      :customer_name=>"Neymar Silva Junior",
      :city=>"Auckland",
      :region=>"Auckland",
      :service=>"Electricity",
      :name=>"Bosco and Sons",
      :service_plan=>"Electricity Plan",
      :connection_type=>nil,
      :billing_method=>nil,
      :icp_number=>"",
      :moving_date=>"",
      :supplier_commission=>28.0,
      :show_url=>"/applications/291"},
 {:id=>292,
      :application_date=>"29/04/2016",
      :customer_number=>"23223",
      :customer_name=>"Neymar Silva Junior",
      :city=>"Auckland",
      :region=>"Auckland",
      :service=>"Electricity",
      :name=>"Bosco and Sons",
      :service_plan=>"Electricity Plan",
      :connection_type=>nil,
      :billing_method=>nil,
      :icp_number=>"",
      :moving_date=>"",
      :supplier_commission=>21.0,
      :show_url=>"/applications/292"}]

I want to sort this data in two different ways, alphabetically(from A to Z) as well as Recursively(Z to A) according to its attributes in following scenarios:

  1. If the sort parameter is service_plan alphabetically it will sort as per this attribute from A to Z, if recursively then Z to A for this attribute and so on for all attributes.

  2. Id is integer so it should be sorted in increasing or decreasing order.

  3. Moreover the nil value should not through an error and should be present in the result.

Thanks in advance!

Upvotes: 2

Views: 2563

Answers (1)

jan.zikan
jan.zikan

Reputation: 1316

def my_sort(data, attribute, asc = true)
  # Make sure that all elements have attribute we want the data to be sorted by
  return data unless data.all? { |elem| elem.key?(attribute) }

  sorted = data.sort_by { |elem| elem[attribute] }
  asc ? sorted : sorted.reverse
end

my_sort(data, :id) # ascending order by default
my_sort(data, :city, false)

If you want to sort by element that can be missing:

def my_sort(data, attribute, asc = true)
  # Convert to string because of possible nil values      
  sorted = data.sort_by { |elem| elem[attribute].to_s }
  asc ? sorted : sorted.reverse
end

Upvotes: 3

Related Questions