Created
May 28, 2020 18:42
-
-
Save cgytrus/aa70b7772f70e7545fb9de7330c00f47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<LevelSpeed> SortSpeedsModified(List<LevelSpeed> list) { | |
List<LevelSpeed> unsorted = new List<LevelSpeed>(list); | |
List<LevelSpeed> sorted = new List<LevelSpeed>(); | |
unsorted.Sort((speed1, speed2) => speed1.offset.CompareTo(speed2.offset)); | |
int direction = 0; | |
for(int i = 0; i < unsorted.Count; i++) { | |
if(unsorted[i].offset == 0) { | |
direction = Math.Sign(unsorted[i].speed); | |
sorted.Add(unsorted[i]); | |
unsorted.RemoveAt(i); | |
break; | |
} | |
} | |
while(unsorted.Count > 0) { | |
if(direction == 0) break; | |
if(direction == -1) direction = 0; | |
if(direction >= unsorted.Count) break; | |
sorted.Add(unsorted[direction]); | |
int newDirection = Math.Sign(unsorted[direction].speed); | |
unsorted.RemoveAt(direction); | |
direction = newDirection; | |
} | |
sorted.Add(unsorted[0]); | |
unsorted.RemoveAt(direction); | |
return sorted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment