double leem
double leem

Reputation: 65

Sorting maps value within 2 list by value flutter

can i sort about value in map within list?? expect result: [1,2,3,4,6,7]

List a = [
        [
          {'g': 1},
          {'g': 5},
          {'g': 3}
        ],
        [
          {'g': 7},
          {'g': 2}
        ],
        [
          {'g': 4}
        ]
      ];

Upvotes: 0

Views: 132

Answers (1)

Sujan Gainju
Sujan Gainju

Reputation: 4769

  1. Loop through the map of the data a and add the value to a list.
  2. Sort the list

List a = [
                        [
                          {'g': 1},
                          {'g': 5},
                          {'g': 3}
                        ],
                        [
                          {'g': 7},
                          {'g': 2}
                        ],
                        [
                          {'g': 4}
                        ]
                      ];
                      List<int> values = [];
                      for (var o in a) {
                        for (var i in o) {
                          values.add(i['g']);
                        }
                      }
                      values.sort((a, b) => a - b);
                      print(values);

Output:

 [1, 2, 3, 4, 5, 7]

Upvotes: 1

Related Questions