gpt4 book ai didi

dart - Dart按两个属性排序列表

转载 作者:行者123 更新时间:2023-12-03 03:08:32 25 4
gpt4 key购买 nike

我有一个带有相同对象的列表。

我想通过两个属性对它们全部进行排序

名(从a到Z)

第二种类型(数字递增,例如1,2,3 ...)

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'));

for (MyObject _object in list) {
print(_object.name + ' ' + _object.type);
}

RegExp regExp = new RegExp(
r"#'?.*",
);

list.sort((a, b) {
int _a = int.parse(regExp.stringMatch(a.type).replaceAll('#', ''));
int _b = int.parse(regExp.stringMatch(b.type).replaceAll('#', ''));

return _a
.compareTo(_b); //to get the order other way just switch `adate & bdate`
});

list.sort((a, b) => a.name.compareTo(b.name));
}

class MyObject {
String name;
String type;

MyObject(this.name, this.type);
}

我想要这样的输出,
Apple #1
Apple #8
Apple #16
Banana #2
Banana #5
...

我试图使用正则表达式方法解析数字(来自“类型为#(number)的定义”)

但是,如果我以此对列表进行排序,则无法将列表从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);
}

输出:

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

关于dart - Dart按两个属性排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61343000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com