diff --git a/app/services/exports/export_distributions_csv_service.rb b/app/services/exports/export_distributions_csv_service.rb index dae6df149c..64557e3369 100644 --- a/app/services/exports/export_distributions_csv_service.rb +++ b/app/services/exports/export_distributions_csv_service.rb @@ -118,7 +118,7 @@ def base_headers def item_headers return @item_headers if @item_headers - @item_headers = @organization.items.order(:created_at).distinct.select([:created_at, :name]).map(&:name) + @item_headers = @organization.items.select("DISTINCT ON (LOWER(name)) items.name").order("LOWER(name) ASC").map(&:name) end def build_row_data(distribution) diff --git a/spec/services/exports/export_distributions_csv_service_spec.rb b/spec/services/exports/export_distributions_csv_service_spec.rb index 5d31de5c75..89c041d825 100644 --- a/spec/services/exports/export_distributions_csv_service_spec.rb +++ b/spec/services/exports/export_distributions_csv_service_spec.rb @@ -44,7 +44,7 @@ let(:item_id) { duplicate_item.id } let(:item_name) { duplicate_item.name } let(:filters) { {by_item_id: item_id} } - let(:all_org_items) { Item.where(organization:).uniq.sort_by(&:created_at) } + let(:all_org_items) { Item.where(organization:).uniq.sort_by { |item| item.name.downcase } } let(:total_item_quantities) do template = all_org_items.pluck(:name).index_with(0)