按两个属性对Dart列表进行排序
我有一个相同对象的列表 我想用两个属性对它们进行排序 名字(从a到Z) 第二种类型(升序数字,如1,2,3…) 我尝试使用正则表达式方法解析数字(来自“类型的定义”#(数字)”)按两个属性对Dart列表进行排序,dart,Dart,我有一个相同对象的列表 我想用两个属性对它们进行排序 名字(从a到Z) 第二种类型(升序数字,如1,2,3…) 我尝试使用正则表达式方法解析数字(来自“类型的定义”#(数字)”) 但如果我按此对列表进行排序,则无法将列表从a排序到Z(名称)您只需排序一次并定义排序方法,以便在名称相同时比较类型: void main() { List<MyObject> list = List(); list.add(MyObject('Apple', 'definition of type
但如果我按此对列表进行排序,则无法将列表从a排序到Z(名称)您只需排序一次并定义排序方法,以便在名称相同时比较类型:
void main() {
List<MyObject> list = List();
list.add(MyObject('Apple', 'definition of type #1'));
list.add(MyObject('Strawberry', 'definition of type #8'));
list.add(MyObject('Banana', 'definition of type #2'));
list.add(MyObject('Orange', 'definition of type #3'));
list.add(MyObject('Kiwi', 'definition of type #1'));
list.add(MyObject('Peach', 'definition of type #4'));
list.add(MyObject('Orange', 'definition of type #1'));
list.add(MyObject('Apple', 'definition of type #8'));
list.add(MyObject('Peach', 'definition of type #2'));
list.add(MyObject('Strawberry', 'definition of type #17'));
list.add(MyObject('Peach', 'definition of type #1'));
list.add(MyObject('Banana', 'definition of type #5'));
list.add(MyObject('Apple', 'definition of type #16'));
list.add(MyObject('Strawberry', 'definition of type #7'));
RegExp regExp = new RegExp(
r"#'?.*",
);
list.sort((a, b) {
int compare = a.name.compareTo(b.name);
if (compare == 0) {
int _a = int.parse(regExp.stringMatch(a.type).replaceAll('#', ''));
int _b = int.parse(regExp.stringMatch(b.type).replaceAll('#', ''));
return _a.compareTo(_b);
} else {
return compare;
}
});
for (MyObject _object in list) {
print(_object.name + ' ' + _object.type);
}
}
class MyObject {
String name;
String type;
MyObject(this.name, this.type);
}
您只需排序一次并定义排序方法,以便在名称相同时比较类型:
void main() {
List<MyObject> list = List();
list.add(MyObject('Apple', 'definition of type #1'));
list.add(MyObject('Strawberry', 'definition of type #8'));
list.add(MyObject('Banana', 'definition of type #2'));
list.add(MyObject('Orange', 'definition of type #3'));
list.add(MyObject('Kiwi', 'definition of type #1'));
list.add(MyObject('Peach', 'definition of type #4'));
list.add(MyObject('Orange', 'definition of type #1'));
list.add(MyObject('Apple', 'definition of type #8'));
list.add(MyObject('Peach', 'definition of type #2'));
list.add(MyObject('Strawberry', 'definition of type #17'));
list.add(MyObject('Peach', 'definition of type #1'));
list.add(MyObject('Banana', 'definition of type #5'));
list.add(MyObject('Apple', 'definition of type #16'));
list.add(MyObject('Strawberry', 'definition of type #7'));
RegExp regExp = new RegExp(
r"#'?.*",
);
list.sort((a, b) {
int compare = a.name.compareTo(b.name);
if (compare == 0) {
int _a = int.parse(regExp.stringMatch(a.type).replaceAll('#', ''));
int _b = int.parse(regExp.stringMatch(b.type).replaceAll('#', ''));
return _a.compareTo(_b);
} else {
return compare;
}
});
for (MyObject _object in list) {
print(_object.name + ' ' + _object.type);
}
}
class MyObject {
String name;
String type;
MyObject(this.name, this.type);
}
要进行完整的比较,您应该实现Comparable
compareTo()
方法来按一个或多个属性进行排序
请参阅下面的完整示例:
void main() {
List<MyObject> list = List();
list.add(MyObject('Apple', 'definition of type #1'));
list.add(MyObject('Strawberry', 'definition of type #8'));
list.add(MyObject('Banana', 'definition of type #2'));
list.add(MyObject('Orange', 'definition of type #3'));
list.add(MyObject('Kiwi', 'definition of type #1'));
list.add(MyObject('Peach', 'definition of type #4'));
list.add(MyObject('Orange', 'definition of type #1'));
list.add(MyObject('Apple', 'definition of type #8'));
list.add(MyObject('Peach', 'definition of type #2'));
list.add(MyObject('Strawberry', 'definition of type #17'));
list.add(MyObject('Peach', 'definition of type #1'));
list.add(MyObject('Banana', 'definition of type #5'));
list.add(MyObject('Apple', 'definition of type #16'));
list.add(MyObject('Strawberry', 'definition of type #7'));
list.sort();
for (MyObject _object in list) {
print(_object);
}
}
abstract class Comparable {
int compareTo(Object obj){
}
}
class MyObject implements Comparable<MyObject> {
static final RegExp _regExp = RegExp(r'\D+');
final String name;
final String type;
MyObject(this.name, this.type);
@override
int compareTo(MyObject other) {
int result = name?.compareTo(other?.name) ?? 0;
if (result == 0) {
int type1 = int.tryParse(type?.replaceAll(_regExp, '') ?? 0);
int type2 = int.tryParse(other?.type?.replaceAll(_regExp, '') ?? 0);
result = type1.compareTo(type2);
}
return result;
}
@override
String toString() => '$name $type';
}
要进行完整的比较,您应该实现Comparable
compareTo()
方法来按一个或多个属性进行排序
请参阅下面的完整示例:
void main() {
List<MyObject> list = List();
list.add(MyObject('Apple', 'definition of type #1'));
list.add(MyObject('Strawberry', 'definition of type #8'));
list.add(MyObject('Banana', 'definition of type #2'));
list.add(MyObject('Orange', 'definition of type #3'));
list.add(MyObject('Kiwi', 'definition of type #1'));
list.add(MyObject('Peach', 'definition of type #4'));
list.add(MyObject('Orange', 'definition of type #1'));
list.add(MyObject('Apple', 'definition of type #8'));
list.add(MyObject('Peach', 'definition of type #2'));
list.add(MyObject('Strawberry', 'definition of type #17'));
list.add(MyObject('Peach', 'definition of type #1'));
list.add(MyObject('Banana', 'definition of type #5'));
list.add(MyObject('Apple', 'definition of type #16'));
list.add(MyObject('Strawberry', 'definition of type #7'));
list.sort();
for (MyObject _object in list) {
print(_object);
}
}
abstract class Comparable {
int compareTo(Object obj){
}
}
class MyObject implements Comparable<MyObject> {
static final RegExp _regExp = RegExp(r'\D+');
final String name;
final String type;
MyObject(this.name, this.type);
@override
int compareTo(MyObject other) {
int result = name?.compareTo(other?.name) ?? 0;
if (result == 0) {
int type1 = int.tryParse(type?.replaceAll(_regExp, '') ?? 0);
int type2 = int.tryParse(other?.type?.replaceAll(_regExp, '') ?? 0);
result = type1.compareTo(type2);
}
return result;
}
@override
String toString() => '$name $type';
}
这回答了你的问题吗?这回答了你的问题吗?
Apple definition of type #1
Apple definition of type #8
Apple definition of type #16
Banana definition of type #2
Banana definition of type #5
Kiwi definition of type #1
Orange definition of type #1
Orange definition of type #3
Peach definition of type #1
Peach definition of type #2
Peach definition of type #4
Strawberry definition of type #7
Strawberry definition of type #8
Strawberry definition of type #17