applySort method

List<Map<String, dynamic>>? applySort(
  1. ItemSort? itemSort,
  2. String keyId,
  3. List<Map<String, dynamic>>? list
)

Implementation

List<Map<String, dynamic>>? applySort(
    ItemSort? itemSort, String keyId, List<Map<String, dynamic>>? list) {
  _currentSort = itemSort;
  try {
    if (itemSort != null && list != null && list.isNotEmpty) {
      final TypeData? typeData = itemSort.line?.typeData;

      /// Maintain a consistent order for the list
      Iterable<Map<String, dynamic>> temp;
      if (typeData is TDBoolean) {
        temp = list.sortedBy((e) => e[keyId] == true ? 1 : 0);
      } else {
        temp = list.sortedBy((e) => e[keyId]);
      }

      if (typeData is TDString || typeData is TDNotString) {
        list = _sort(temp as List<Map<String, dynamic>>, itemSort, '',
            typeData: typeData);
      } else if (typeData is TDNumber) {
        list = _sort(temp as List<Map<String, dynamic>>, itemSort, 0,
            typeData: typeData);
      } else if (typeData is TDBoolean) {
        list = _sort(temp as List<Map<String, dynamic>>, itemSort, false,
            typeData: typeData);
      } else if (typeData is TDDateTimestamp) {
        list = _sort(temp as List<Map<String, dynamic>>, itemSort, 0,
            typeData: typeData);
      } else if (typeData is TDDateString) {
        list = _sort(temp as List<Map<String, dynamic>>, itemSort, '',
            typeData: typeData);
      } else {
        list = _sort(temp as List<Map<String, dynamic>>, itemSort, null,
            typeData: typeData);
      }
    }
  } catch (error, stackTrace) {
    UtilsSentry.reportError(error, stackTrace);
  }
  return list;
}