简体   繁体   中英

Dart - Best sort Algorithm for pushing a list of objects to a list of First Class Lists

I have a list of objects that are retrieved from a DB. The object looks like this:

class MonthlyFinancePlan {

  final int id;
  final DateTime date;
  final double incomeAfterTax;
  final double totalToPayOut;
  final double totalRemainingForMonth;
  final Map<String, dynamic> items;

  MonthlyFinancePlan({ this.id, this.date, this.incomeAfterTax, this.totalToPayOut, this.totalRemainingForMonth, this.items });

  MonthlyFinancePlan.fromEntity(MonthlyFinancePlanEntity monthlyFinancePlanEntity):
    this.id = monthlyFinancePlanEntity.id,
    this.date = DateTime.parse(monthlyFinancePlanEntity.date),
    this.incomeAfterTax = monthlyFinancePlanEntity.incomeAfterTax.toDouble(),
    this.totalToPayOut = monthlyFinancePlanEntity.totalToPayOut.toDouble(),
    this.totalRemainingForMonth = monthlyFinancePlanEntity.moneyRemainingForMonth.toDouble(),
    this.items = monthlyFinancePlanEntity.items != null ? json.decode(monthlyFinancePlanEntity.items) : Map();
}

I need to sort these by date.year and then pass them into a first class List, I'd like to create a List of these First class lists so that all the MonthlyFinancePlan objects that are from the year 2020 are sorted and contained within the first class list, same for 2021, etc.

The first class list looks like this:

class YearlyFinancePlan {

  List<MonthlyFinancePlan> _monthlyFinancePlanList;

  int _year;
  double _totalIncomeForYear;
  double _totalOutGoingsForYear;

  List<MonthlyFinancePlan> get items {
    return this._monthlyFinancePlanList;
  }

  int get year {
    return this._year;
  }

  double get totalIncomeForYear {
    return this._totalIncomeForYear;
  }

  double get totalOutgoingsForYear {
    return this._totalOutGoingsForYear;
  }

  YearlyFinancePlan(this._monthlyFinancePlanList) {
    this._year = this._monthlyFinancePlanList.first.date.year;
    this._totalIncomeForYear = this._setTotalIncomeFromList(this._monthlyFinancePlanList);
    this._totalOutGoingsForYear = this._setTotalOutGoingsForYear(this._monthlyFinancePlanList);
  }

  double _setTotalIncomeFromList(List<MonthlyFinancePlan> monthlyFinancePlanList) {
    double totalIncome;

    monthlyFinancePlanList.forEach((plan) => totalIncome += plan.incomeAfterTax);
    return totalIncome;
  }

  double _setTotalOutGoingsForYear(List<MonthlyFinancePlan> monthlyFinancePlanList) {
    double totalOutgoings;

    monthlyFinancePlanList.forEach((plan) => totalOutgoings += plan.totalToPayOut);
    return totalOutgoings;
  }
}

My question is, what sort algorithm would be best suited for what I need? I don't have any code to show as I don't know what sort algorithm to use. I'm not looking for anyone to write my code, but more to guide me through it.

Any help would be greatly appreciated

I've created a Mapper that checks if the MonthlyPlanner.date.year exists as a key in a standard Dart Map and adds it if it doesn't exist. Once the check is complete, it also calls the addMonthlyPlan method to add the entry to the MonthlyPlan to the correct YearlyPlan like so:

class FinancePlanMapper {

  static Map<int, YearlyFinancePlan> toMap(List<MonthlyFinancePlan> planList) {
    Map<int, YearlyFinancePlan> planMap = Map();

    planList.forEach((monthlyPlan) {
      planMap.putIfAbsent(monthlyPlan.date.year, () => YearlyFinancePlan(List()));
      planMap[monthlyPlan.date.year].addMonthlyPlan(monthlyPlan);
    });

    return planMap;
  }

}

I'm not too sure whether it's the most efficient way of sorting but I plan to refactor it as much as possible. I've also updated the YearlyFinancePlan object so that it doesn't initialise any fields on construction, which would cause the object to throw an error when being initialised with an empty list:

class YearlyFinancePlan {

  List<MonthlyFinancePlan> _monthlyFinancePlanList;

  List<MonthlyFinancePlan> get items {
    return this._monthlyFinancePlanList;
  }

  int get year {
    return this.items.first.date.year;
  }

  double get totalIncomeForYear {
    return this._setTotalIncomeFromList(this._monthlyFinancePlanList);
  }

  double get totalOutgoingsForYear {
    return this._setTotalOutGoingsForYear(this._monthlyFinancePlanList);
  }

  YearlyFinancePlan(this._monthlyFinancePlanList);

  void addMonthlyPlan(MonthlyFinancePlan plan) {
    this._monthlyFinancePlanList.add(plan);
  }

  double _setTotalIncomeFromList(List<MonthlyFinancePlan> monthlyFinancePlanList) {
    double totalIncome = 0;

    monthlyFinancePlanList.forEach((plan) => totalIncome += plan.incomeAfterTax);
    return totalIncome;
  }

  double _setTotalOutGoingsForYear(List<MonthlyFinancePlan> monthlyFinancePlanList) {
    double totalOutgoings = 0;

    monthlyFinancePlanList.forEach((plan) => totalOutgoings += plan.totalToPayOut);
    return totalOutgoings;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM