List colour= ["peach","red","yellow","green","black"];
//How to use both .sort and .toSet methods together in Flutter Dart.
CodePudding user response:
Sort method return type void so you can not use .toSet Method together. But after applying sort you can use .toSet method it works as you expected
Try this
List colour= ["peach","red","yellow","green","black"];
colour.sort((a, b) => a.length.compareTo(b.length));
colour.toSet().toList();
CodePudding user response:
Try this extension:
extension SortEX on List {
List sortAndSet() {
sort();
return toSet().toList();
}
}
the result you get :
//[black, green, peach, red, yellow]
CodePudding user response:
Sort the List
first, then toSet()
List colour= ["peach","red","yellow","green","black", "black"];
colour.sort();
var res = colour.toSet();
print(res); //{black, green, peach, red, yellow}