sortNumeric function

List<String> sortNumeric(
  1. List<String> array, {
  2. bool descending = false,
})

When sorting an array consisting of strings, it sorts first by the numbers contained in the strings, not by their names. For example, given the array ['1-a', '100-a', '10-a', '2-a'], it returns ['1-a', '2-a', '10-a', '100-a'] with the smaller numbers at the front.

Implementation

List<String> sortNumeric(List<String> array, {bool descending = false}) {
  final List<String> result = List<String>.from(array)..sort(compareNatural);

  return descending ? result.reversed.toList() : result;
}