From 6cb8a719afa17cc4dd1673d1ba9d0f9038dcfae8 Mon Sep 17 00:00:00 2001
From: EliteMasterEric <ericmyllyoja@gmail.com>
Date: Tue, 17 Oct 2023 17:27:11 -0400
Subject: [PATCH] Additional cleanup on difficulty sort order (tested and
 working!)

---
 source/funkin/play/song/Song.hx | 28 ++--------------------------
 source/funkin/ui/story/Level.hx | 27 ++-------------------------
 source/funkin/util/Constants.hx |  5 +++++
 3 files changed, 9 insertions(+), 51 deletions(-)

diff --git a/source/funkin/play/song/Song.hx b/source/funkin/play/song/Song.hx
index 33363e1ff..f996d75ef 100644
--- a/source/funkin/play/song/Song.hx
+++ b/source/funkin/play/song/Song.hx
@@ -1,5 +1,6 @@
 package funkin.play.song;
 
+import funkin.util.SortUtil;
 import flixel.sound.FlxSound;
 import openfl.utils.Assets;
 import funkin.modding.events.ScriptEvent;
@@ -252,32 +253,7 @@ class Song implements IPlayStateScriptedClass implements IRegistryEntry<SongMeta
       return difficulty.variation == variationId;
     });
 
-    // sort the difficulties, since they may be out of order in the chart JSON
-    // maybe be careful of lowercase/uppercase?
-    // also used in Level.listDifficulties()!!
-    var diffMap:Map<String, Int> = new Map<String, Int>();
-    for (difficulty in diffFiltered)
-    {
-      var num:Int = 0;
-      switch (difficulty)
-      {
-        case 'easy':
-          num = 0;
-        case 'normal':
-          num = 1;
-        case 'hard':
-          num = 2;
-        case 'erect':
-          num = 3;
-        case 'nightmare':
-          num = 4;
-      }
-      diffMap.set(difficulty, num);
-    }
-
-    diffFiltered.sort(function(a:String, b:String) {
-      return (diffMap.get(a) ?? 0) - (diffMap.get(b) ?? 0);
-    });
+    diffFiltered.sort(SortUtil.defaultsThenAlphabetically.bind(Constants.DEFAULT_DIFFICULTY_LIST));
 
     return diffFiltered;
   }
diff --git a/source/funkin/ui/story/Level.hx b/source/funkin/ui/story/Level.hx
index c976cbfce..e86241277 100644
--- a/source/funkin/ui/story/Level.hx
+++ b/source/funkin/ui/story/Level.hx
@@ -1,5 +1,6 @@
 package funkin.ui.story;
 
+import funkin.util.SortUtil;
 import flixel.FlxSprite;
 import flixel.util.FlxColor;
 import funkin.play.song.Song;
@@ -155,31 +156,7 @@ class Level implements IRegistryEntry<LevelData>
       }
     }
 
-    // sort the difficulties, since they may be out of order in the chart JSON
-    // also copy/pasted to Song.listDifficulties()!
-    var diffMap:Map<String, Int> = new Map<String, Int>();
-    for (difficulty in difficulties)
-    {
-      var num:Int = 0;
-      switch (difficulty)
-      {
-        case 'easy':
-          num = 0;
-        case 'normal':
-          num = 1;
-        case 'hard':
-          num = 2;
-        case 'erect':
-          num = 3;
-        case 'nightmare':
-          num = 4;
-      }
-      diffMap.set(difficulty, num);
-    }
-
-    difficulties.sort(function(a:String, b:String) {
-      return diffMap.get(a) - diffMap.get(b);
-    });
+    difficulties.sort(SortUtil.defaultsThenAlphabetically.bind(Constants.DEFAULT_DIFFICULTY_LIST));
 
     // Filter to only include difficulties that are present in all songs
     for (songIndex in 1...songList.length)
diff --git a/source/funkin/util/Constants.hx b/source/funkin/util/Constants.hx
index c606e469f..b27a7d2f5 100644
--- a/source/funkin/util/Constants.hx
+++ b/source/funkin/util/Constants.hx
@@ -121,6 +121,11 @@ class Constants
    */
   public static final DEFAULT_DIFFICULTY:String = 'normal';
 
+  /**
+   * Default list of difficulties for charts.
+   */
+  public static final DEFAULT_DIFFICULTY_LIST:Array<String> = ['easy', 'normal', 'hard'];
+
   /**
    * Default player character for charts.
    */