I'm essentially looking for another way to sort a string list besides the.sort() Using that for my purpose gives me type '_SecItem' is not a subtype of type 'Comparable<dynamic>'
I'm trying to sort a string list by the numerical numbers in front of the strings. Something like:
List<String> hi = ['05stack', '03overflow', '01cool','04is', '02uToo'];
to this:
['01cool', '02uToo', '03overflow', '04is', '05stack']
I can sort easily using sort().
var List<String> hi = ['05stack', '03overflow', '01cool', '04is', '02uToo'];
hi.sort();
hi.forEach((element) {
print(element);
});
it prints:
01cool
02uToo
03overflow
04is
05stack
I am not getting any error of
type '_SecItem' is not a subtype of type 'Comparable<dynamic>'
With a function to extract the number like
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);
}
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.