我正在尝试使用以下功能对称为“帖子”的对象列表进行排序。它似乎没有按我预期的那样工作,我不确定为什么。在调用函数之前,“帖子”中的标题值是66666666666,新的,卢克·凯迪,罗里的溜冰场,新的帖子。之后按标题排序:66666666666,卢克·凯迪(Luke Kady),新任职,新职位,罗里的溜冰场。还尝试添加.toLowerCaase()
中的标题,并得到相同的结果。
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));
}
}
答案 0 :(得分:1)
您可以手动检查compareTo值并决定要做什么。
在下面的示例中,我们使用反射(镜像),因此我们可以指定一个属性(不需要反射,也可以避免反射)。
以下代码可帮助您入门:
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));
}
输出:
---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
答案 1 :(得分:0)
替代方式:
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}';
}
}
结果:
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]