I am trying to sort a list of objects called "posts" with the following function. It does not seem to be working as I expect and I am not sure why. Title values in "posts" BEFORE called on function are 66666666666, new one, Luke Kady, Rory's rink, new post. AFTER sorted on title: 66666666666, Luke Kady, new one, new post, Rory's rink. Also tried adding in .toLowerCaase()
for titles and got same results.
void sortPosts(String sortBy) {
if (sortBy == "Title") {
posts.sort((a, b) => a.title[0].compareTo(b.title[0]));
} else {
posts.sort((a, b) => a.price.compareTo(b.price));
}
}
You could manually check the compareTo value and decide what to do.
In the below example we use reflection (mirrors) so we can specify a property (reflection isn't necessary, and can be avoided as well).
Here is some code to help you get started:
class Post {
String title;
String author;
Post({this.title = "N/A", this.author = "N/A"});
// partially applicable sorter
static Function(Post, Post) sorter(int sortOrder, String property) {
int handleSortOrder(int sortOrder, int sort) {
if (sortOrder == 1) {
// a is before b
if (sort == -1) {
return -1;
} else if (sort > 0) {
// a is after b
return 1;
} else {
// a is same as b
return 0;
}
} else {
// a is before b
if (sort == -1) {
return 1;
} else if (sort > 0) {
// a is after b
return 0;
} else {
// a is same as b
return 0;
}
}
}
return (Post a, Post b) {
switch (property) {
case "title":
int sort = a.title.compareTo(b.title);
return handleSortOrder(sortOrder, sort);
case "author":
int sort = a.author.compareTo(b.author);
return handleSortOrder(sortOrder, sort);
default:
break;
}
};
}
// sortOrder = 1 ascending | 0 descending
static void sortPosts(List<Post> posts, {int sortOrder = 1, String property = "title"}) {
switch (property) {
case "title":
posts.sort(sorter(sortOrder, property));
break;
case "author":
posts.sort(sorter(sortOrder, property));
break;
default:
print("Unrecognized property $property");
}
}
}
void main() {
List<Post> posts = [
new Post(title: "bart loves ice cream", author: "bart"),
new Post(title: "alex loves ice cream", author: "alex"),
new Post(title: "carl loves ice cream", author: "carl")
];
print("---Sorted by title Ascending(default) order---");
Post.sortPosts(posts);
posts.forEach((p) => print(p.title));
print("---Sorted by title Descending order----");
Post.sortPosts(posts, sortOrder: 0);
posts.forEach((p) => print(p.title));
print("---Sorted by author Ascending order---");
Post.sortPosts(posts, property: "author");
posts.forEach((p) => print(p.author));
}
Output:
---Sorted by title Ascending(default) order---
alex loves ice cream
bart loves ice cream
carl loves ice cream
---Sorted by title Descending order----
carl loves ice cream
bart loves ice cream
alex loves ice cream
---Sorted by author Ascending order---
alex
bart
carl
Alternative way:
import 'package:queries/collections.dart';
void main() {
var posts = getPosts();
var sortBy = 'author';
var sorted = sortPosts(posts, sortBy);
print('Ascending by ${sortBy}');
print(sorted);
//
sortBy = 'title';
print('Descending by ${sortBy}');
sorted = sortPosts(posts, sortBy, false);
print(sorted);
}
List<Post> sortPosts(List<Post> posts, String sortBy, [bool asc = true]) {
var keySelector;
switch (sortBy.toLowerCase()) {
case 'author':
keySelector = (Post p) => p.author;
break;
case 'title':
keySelector = (Post p) => p.title;
break;
default:
throw ArgumentError(sortBy);
}
if (asc) {
return Collection(posts).orderBy<String>(keySelector).toList();
} else {
return Collection(posts).orderByDescending<String>(keySelector).toList();
}
}
List<Post> getPosts() {
var result = <Post>[];
for (var i = 0; i < 3; i++) {
var post = Post('Author ${i}', ' Title ${i}');
result.add(post);
}
return result;
}
class Post {
String author;
String title;
Post(this.author, this.title);
String toString() {
return '${author}: ${title}';
}
}
Result:
Ascending by author [Author 0: Title 0, Author 1: Title 1, Author 2: Title 2] Descending by title [Author 2: Title 2, Author 1: Title 1, Author 0: Title 0]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.