Skip to content

Instantly share code, notes, and snippets.

@Simpler1
Created March 6, 2019 21:20
Show Gist options
  • Save Simpler1/dc77acd6e4cfce608796a51cda3ee9ad to your computer and use it in GitHub Desktop.
Save Simpler1/dc77acd6e4cfce608796a51cda3ee9ad to your computer and use it in GitHub Desktop.
orderByOtherList
main() {
List myList = [
{'id': 'red', 'test': 'some data'},
{'id': 'green', 'other': 'some other data'},
{'id': 'blue'},
];
List myOrderList = [
{'id': 'red', 'order': 1},
{'id': 'green', 'order': 0},
{'id': 'blue', 'order': 2},
];
List orderByOtherList(List theList, String commonField, List orderList, String orderfield) {
List listOut = [];
orderList.sort((a, b) => a[orderfield].compareTo(b[orderfield]));
orderList.forEach((o) => listOut.add(theList.firstWhere((m) => m[commonField] == o[commonField])));
return listOut;
}
print(myList);
print(orderByOtherList(myList, 'id', myOrderList, 'order'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment