我本质上是在寻找除.sort()之外的另一种对字符串列表进行排序的方法将其用于我的目的给了我type '_SecItem' is not a subtype of type 'Comparable<dynamic>'
我正在尝试通过字符串前面的数字对字符串列表进行排序。类似于:
List<String> hi = ['05stack', '03overflow', '01cool','04is', '02uToo'];
至此:
['01cool', '02uToo', '03overflow', '04is', '05stack']
var List<String> hi = ['05stack', '03overflow', '01cool', '04is', '02uToo'];
hi.sort();
hi.forEach((element) {
print(element);
});
它打印:
01cool
02uToo
03overflow
04is
05stack
我没有收到任何错误
type '_SecItem' is not a subtype of type 'Comparable<dynamic>'
int extractNumber(String srt){
RegExp regex = new RegExp(r"(\d+)");
return regex.allMatches(srt).toList().map((m){
return srt.substring(m.start, m.end);
}).toList().map((v)=>int.parse(v)).first;
}
void main(){
List<String> test = ['05stack', '03overflow', '01cool','04is', '02uToo'];
test.sort((a, b)=>extractNumber(a)>extractNumber(b) ? 1 : -1);
print(test);
}