diff --git a/changelogs/unreleased/zj-sort-templates.yml b/changelogs/unreleased/zj-sort-templates.yml
new file mode 100644
index 0000000000000000000000000000000000000000..443c4355890382d7a849aaea5284347d7779fff5
--- /dev/null
+++ b/changelogs/unreleased/zj-sort-templates.yml
@@ -0,0 +1,5 @@
+---
+title: Sort templates in the dropdown
+merge_request:
+author:
+type: fixed
diff --git a/lib/gitlab/template/base_template.rb b/lib/gitlab/template/base_template.rb
index 7ebec8e2cff245ee8e12b6512225243d6e3a246b..7393574ac136161aae9ea67f3b77c2d21f372cdf 100644
--- a/lib/gitlab/template/base_template.rb
+++ b/lib/gitlab/template/base_template.rb
@@ -18,6 +18,10 @@ module Gitlab
         { name: name, content: content }
       end
 
+      def <=>(other)
+        name <=> other.name
+      end
+
       class << self
         def all(project = nil)
           if categories.any?
@@ -58,7 +62,7 @@ module Gitlab
           directory = category_directory(category)
           files = finder(project).list_files_for(directory)
 
-          files.map { |f| new(f, project) }
+          files.map { |f| new(f, project) }.sort
         end
 
         def category_directory(category)
diff --git a/spec/lib/gitlab/template/gitlab_ci_yml_template_spec.rb b/spec/lib/gitlab/template/gitlab_ci_yml_template_spec.rb
index 6541326d1def4b20e264aa043640bf4d91d43abb..e2fa76522bccdb6a7d8749d2ed16f081efa863dd 100644
--- a/spec/lib/gitlab/template/gitlab_ci_yml_template_spec.rb
+++ b/spec/lib/gitlab/template/gitlab_ci_yml_template_spec.rb
@@ -30,6 +30,14 @@ describe Gitlab::Template::GitlabCiYmlTemplate do
     end
   end
 
+  describe '.by_category' do
+    it 'returns sorted results' do
+      result = described_class.by_category('General')
+
+      expect(result).to eq(result.sort)
+    end
+  end
+
   describe '#content' do
     it 'loads the full file' do
       gitignore = subject.new(Rails.root.join('vendor/gitlab-ci-yml/Ruby.gitlab-ci.yml'))
@@ -38,4 +46,14 @@ describe Gitlab::Template::GitlabCiYmlTemplate do
       expect(gitignore.content).to start_with('#')
     end
   end
+
+  describe '#<=>' do
+    it 'sorts lexicographically' do
+      one = described_class.new('a.gitlab-ci.yml')
+      other = described_class.new('z.gitlab-ci.yml')
+
+      expect(one.<=>(other)).to be(-1)
+      expect([other, one].sort).to eq([one, other])
+    end
+  end
 end