Search code examples
flutterdartnumbershighest

flutter finding out the title of the ToDoItem that has the highest price


To-Do App

class ToDoItem{
  final String id;
  final String title;
  final Color color;
  final int price;
  final IconData icon;
  final String todoListId;

  const ToDoItem({
    @required this.color,
    @required this.id,
    @required this.todoListId,
    @required this.price,
    @required this.title,
    @required this.icon,
  });}

I am trying to find the title of the ToDoItem which has the highest price

And the ToDoItem is being added to this:

List<ToDoItem> toDo= [];

Solution

  • void main(){
      
    
      List<ToDoItem> _myList = [];
      _myList.add(ToDoItem('10',100));
      
      _myList.add(ToDoItem('30',400));
      _myList.add(ToDoItem('40',2));
      
      _myList.add(ToDoItem('20',200));
      
      _myList.sort((a,b)=>a.price.compareTo(b.price));
      
      for(ToDoItem items in _myList)
        print(items.id + ',' + items.price.toString());
    }
    
      class ToDoItem{
      final String id;
      final int price;
    
      const ToDoItem(
       this.id,
       this.price,
      );
      }
    

    This is the code which i used in dart to sort your list . YOu could use the sorting logic from in here . it sorts your list in ascending order acc to price .