so i want to make rating box like the image below, but for the rating value it hasn't been sorted yet, plus it's not int/double but string. What code is suitable for this ?
i want it to result High rating 5 to lowest 1
i will be glad if you give some example code
don't mind the linear progress bar
json data
"all_rating": [
{
"rating": "1",
},
{
"rating": "2",
},
{
"rating": "4",
},
{
"rating": "4.5",
},
{
"rating": "5",
}
],
CodePudding user response:
You can use double.parse(<any string containing double>)
to convert string to double and then sort them like below.
// this is list of double that you got from map.
List<double> doubleList = [1,0.5,4,3,2];
// sort the list using sort function.
doubleList.sort();
CodePudding user response:
Use double.parse(<any string containing double>)
to convert string to double and then sort accordingly.
To sort, custom compare to function needs to be given
list.sort((a, b) => a.rating.compareTo(b.rating));
CodePudding user response:
I think this sorting system helps you to get your solution.
List<Map> allRating = [
{
"rating": "1",
},
{
"rating": "2",
},
{
"rating": "4",
},
{
"rating": "4.5",
},
{
"rating": "5",
}
];
allRating.sort((a, b) => (b['rating']).compareTo(a['rating'])); /// sort List<Map<String,dynamic>>
print(allRating); /// output -> [{rating: 5}, {rating: 4.5}, {rating: 4}, {rating: 2}, {rating: 1}]