I want to sort the list twice.
Example list (here is an example list with models):
[Model("A", 5), Model("C", 3), Model("B", 7) Model("F", 5), Model("D", 5)]
What I want is to sort alphabetically and then numerically. If I do this, I get the following result:
[Model("C", 3), Model("A", 5), Model("D", 5) Model("F", 5), Model("B", 7)]
Else:
Sort alphabetically:
[Model("A", 5), Model("B", 7), Model("C", 3) Model("D", 5), Model("F", 5)]
Sort numerically:
[Model("C", 3), Model("A", 5), Model("F", 5) Model("D", 5), Model("B", 7)]
My code:
Stream<List<T>> function<T>({
…
int Function(T lhs, T rhs) sort,
}) {
…
List<T> result = …;
result.sort(sort);
}
Stream<List<Model>> stream() {
return function(
…
sort: (a, b) => a.alphabet.toLowerCase().compareTo(b.alphabetc.toLowerCase())
);
}
Feel free to leave a comment if you need more information.
How to sort a list twice? I would appreciate any help. Thank you in advance!
2
Answers
You can call it twice after each other like this:
in your case:
result :
Try the following code, first, implement the
Comparable
class on youModel
class, and implement thecompareTo
method like this:Then, taking this example
Trying to sort it will get you to your desired result: