
class SortingInformation {
  String deliveryId;
  int position;

  SortingInformation({required this.deliveryId, required this.position});

  static Map<String, dynamic> toJson(SortingInformation info) {
    return {"delivery_id": info.deliveryId, "position": info.position};
  }

  static SortingInformation fromJson(Map<String, dynamic> json) {
    return SortingInformation(
      deliveryId: json["delivery_id"].toString(),
      position: json["position"],
    );
  }
}

class SortingInformationContainer {
  List<SortingInformation> sorting;

  SortingInformationContainer({required this.sorting});

  void sort() {
    sorting.sort((a, b) => a.position.compareTo(b.position));
  }

  static SortingInformationContainer fromJson(Map<String, dynamic> json) {
    SortingInformationContainer container = SortingInformationContainer(
      sorting: [],
    );

    for (final info in json["sorting"]) {
      container.sorting.add(SortingInformation.fromJson(info));
    }

    return container;
  }

  Map<String, dynamic> toJson() {
    return {
      "sorting":
          sorting.map((info) => SortingInformation.toJson(info)).toList(),
    };
  }

  SortingInformationContainer copyWith({List<SortingInformation>? sorting}) {
    return SortingInformationContainer(
      sorting: sorting ?? this.sorting,
    );
  }
}
