sortData method

List<DataItem> sortData(
  1. List<DataSort> parameters
)

Part Extension of List<DataItem> to sort data

Implementation

List<DataItem> sortData(List<DataSort> parameters) {
  if (parameters.isNotEmpty) {
    List<List<DataItem>> temp = [this];
    for (int i = 0; i < parameters.length; i++) {
      List separates = List.generate(length, (index) {
        return this[index].get(parameters[i].key);
      }).toSet().toList();
      separates.sort((a, b) {
        if (a == null || b == null) {
          if (a == null) {
            a = 1;
            b = 1;
            return !parameters[i].desc ? a.compareTo(0) : b.compareTo(0);
          } else {
            a = 0;
            b = 0;
            return !parameters[i].desc ? a.compareTo(1) : b.compareTo(1);
          }
        } else if (a is DateTime || b is DateTime) {
          a = DateTimeUtils.toDateTime(a);
          b = DateTimeUtils.toDateTime(b);
          if (a == null) {
            a = 1;
            b = 1;
            return !parameters[i].desc ? a.compareTo(0) : b.compareTo(0);
          }
          if (b == null) {
            a = 0;
            b = 0;
            return !parameters[i].desc ? a.compareTo(1) : b.compareTo(1);
          }
          return !parameters[i].desc ? a.compareTo(b) : b.compareTo(a);
        } else {
          return !parameters[i].desc ? a.compareTo(b) : b.compareTo(a);
        }
      });

      List<List<DataItem>> store = [];
      for (List<DataItem> dTemp in temp) {
        for (dynamic separate in separates) {
          store.add(dTemp.where((element) {
            return element.get(parameters[i].key) == separate;
          }).toList());
        }
      }
      temp = store;
    }
    return temp.expand((element) => element).toList();
  }
  // Set<String> ids = map((e) => e.id).toSet();
  // retainWhere((x) => ids.remove(x.id));

  return this;
}