-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add positional isAscending
to sortedBy
#176
base: master
Are you sure you want to change the base?
Conversation
What's wrong with |
Imagine you have manage a state like table sorts. Today we have to do something like this: final classes = isAscending
? context.watch<ClassStore>().classes.sortedBy(
(clazz) => switch (sortType) {
ClassSort.name => clazz.name,
ClassSort.code => clazz.code,
ClassSort.createdAt => clazz.createdAt.toString(),
},
)
: context.watch<ClassStore>().classes.sortedByDescending(
(clazz) => switch (sortType) {
ClassSort.name => clazz.name,
ClassSort.code => clazz.code,
ClassSort.createdAt => clazz.createdAt.toString(),
},
); Or look for other solutions, like reverse the list, which requires even more computations.
In Ex: final classes = context.watch<ClassStore>().classes.sortedBy(
(clazz) => switch (sortType) {
ClassSort.name => clazz.name,
ClassSort.code => clazz.code,
ClassSort.createdAt => clazz.createdAt.toString(),
},
isAscending, // <- with `isAscending`
); This would make the current |
Especially, when your sort order can be changed, you should use List<Clazz> doSort(List<Clazz> classes, ClassSort sortType, bool isAscending) {
final Comparator<Clazz> comparator = compareBy((clazz) => switch (sortType) {
ClassSort.name => clazz.name,
ClassSort.code => clazz.code,
ClassSort.createdAt => clazz.createdAt.toString(),
});
return classes.sortedWith(isAscending ? comparator : comparator.reverse());
}
/// Creates a comparator using the function to transform value to a [Comparable] instance for comparison.
Comparator<T> compareBy<T>(Comparable Function(T) selector) {
int compareTo(T a, T b) => selector(a).compareTo(selector(b));
return compareTo;
} I took You can find this very example in my "Getting started with Functional Programming" talk from 2022. |
Thank you for the detailed explanation and the alternative approach using sortedWith/Comparator.reverse(). I appreciate the efficiency and elegance of this method, and I understand it aligns well with functional programming principles as mentioned in the video! I still believe there’s a strong case for adding the For many developers, especially those who might not be as familiar with comparators and more advanced functional programming concepts, having a straightforward boolean parameter makes the API more accessible and user-friendly. The boolean I appreciate your consideration of this proposal. I really believe it can add value to dartx by enhancing its usability and aligning it with common patterns, making it easier for developers to adopt and use effectively. I even talked with my team, and they encouraged me to do this PR. Thanks again for your time and for the great work on this package. Loved watching your video, you are inspiring :). |
Refined parameter naming and documentation for consistency with Flutter conventions. SortedList<E> sortedBy(
Comparable Function(E element) selector, {
bool ascending = true,
}) { expect([4, 2, 1, 3].sortedBy((it) => it, ascending: true), [1, 2, 3, 4]);
expect([4, 2, 1, 3].sortedBy((it) => it, ascending: false), [4, 3, 2, 1]); |
I'm fine with landing this One semantic question:
When |
I'd say /// Signature for [DataColumn.onSort] callback.
typedef DataColumnSortCallback = void Function(int columnIndex, bool ascending); Reference: data_table.dart. |
sortedBy
is one of the best extensions ofdartx
.SortedList
class allows us to sort, and uses the internalorder
int to decide whether it is ascending or not.This PR simplifies this process so we can simply decide it based on
isAscending
positional boolean in thesortedBy
extension.This is useful, because without it, we would have to do additional operations bringing either more boilerplate or additional computations. By adding this positional boolean we can make it straightforward.