Created
March 6, 2019 21:20
-
-
Save Simpler1/dc77acd6e4cfce608796a51cda3ee9ad to your computer and use it in GitHub Desktop.
orderByOtherList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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