Skip to content

Instantly share code, notes, and snippets.

@dimasandhk
Created July 22, 2021 10:43
Show Gist options
  • Save dimasandhk/5b8e91d76ddd03f6b4c72a76876b6997 to your computer and use it in GitHub Desktop.
Save dimasandhk/5b8e91d76ddd03f6b4c72a76876b6997 to your computer and use it in GitHub Desktop.
extension Sorting on List<int> {
List<int> sortAsc() {
List<int> list = this; // Selection sort algorithm
int length = this.length;
for (int i = 0; i < length - 1; i++) {
int min = i;
for (int j = i + 1; j < length; j++) {
if (list[j] < list[min]) {
min = j;
}
}
int tmp = list[min];
list[min] = list[i];
list[i] = tmp;
}
return list;
}
}
void main() {
List<int> arr = [3, 2, 1, 4, 5];
print(arr);
List<int> sortedArr = arr.sortAsc();
print(sortedArr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment