简体   繁体   中英

Flutter sort list by value

how i can sort my List by one of my values from list?

here is a example of my list

  List data = [
    {'name': 'Example1', 'value1': 10, 'value2': 0},
    {'name': 'Example2', 'value1': 0, 'value2': 10},
    {'name': 'Example3', 'value1': 15, 'value2': 5},
    {'name': 'Example4', 'value1': 22.5, 'value2': 10},
];

and so I call it

           Column(
            children: data.map((info) {
          return Container(
            child: SizedBox(
              child: Row(
                children: [
                  Text(info['name'],),
                  Text(info['value1'],),
                ],
              ),
            ),
          );
        }).toList())

this is how my list is listed from top to bottom

how can i sort it by value?

How can I hide an entry if the value is 0?

list.sort() takes a comparator function. A comparator takes two values from the list and compares them to see if swapping is required. Based on what you return, you can control how the lists get sorted. When a positive value is returned, swapping occurs otherwise not.

In your case, let's say you want to sort using value1 in increasing order. You need to tell the comparator to return a positive value when a > b. If you want decreasing order, return a positive value when b > a:

List data = [
    {'name': 'Example1', 'value1': 15},
    {'name': 'Example2', 'value1': 10},
    {'name': 'Example3', 'value1': 5},
    {'name': 'Example4', 'value1': 0},
];
  
// sort in place w.r.t. value1
// CompareTo method just returns first value - second value in case of double
// Try returning b['value1'].compareTo(a['value1']) or b['value1'] - a['value1'] and the result should be in descending order w.r.t value1 property.
 
data.sort((a,b) => a['value1'].compareTo(b['value1'])); // You can also write a['value1'] - b['value1']
print(data.toString());
 
// To filter (remove all those elements whose value1 is 0)
List filtered = data.where((a) => a['value1'] != 0).toList(); // Where method takes a function which should return true if you want to keep the item in the filtered list.
print(filtered.toString()); // filtered is the new list with all those elements removed.

and here is the output:

[{name: Example4, value1: 0}, {name: Example3, value1: 5}, {name: Example2, value1: 10}, {name: Example1, value1: 15}]
[{name: Example3, value1: 5}, {name: Example2, value1: 10}, {name: Example1, value1: 15}]

Update:

You can use the filter like this:

Column(
        children: data
        .where((d) => d['value1'] != 0) // <----- Here :)
        .map((info) {
      return Container(
        child: SizedBox(
          child: Row(
            children: [
              Text(info['name'],),
              Text(info['value1'],),
            ],
          ),
        ),
      );
    }).toList())

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM