Home > Blockchain >  Flutter : Compare int with list<Map<String,int>>
Flutter : Compare int with list<Map<String,int>>

Time:06-12

If anyone is able to help, thank you.

This is the list I want to compare with

[{"quote":3,"money":3000}, {"quote":6,"money":7000}, {"quote":8,"money":8000}]

On the previous page, the user enters the amount the purchase will be, for example, $3500. If the number exceeds

3000 then the user needs to present 3 quotes

7000 then the user needs to present 6 quotes

8000 then the user needs to present 8 quotes

I am struggling to find a solution to compare the money amount with the list value to get the respective Map value

If anyone has any suggestion. It will be greatly appreciated

Thank you

CodePudding user response:

I changed the data to a more convenient structure

all the code below will be based on this structure

final data = [{"quote":3,"money":3000}, {"quote":6,"money":7000}, {"quote":8,"money":8000},];

next step is sorting the data,

if the data already sorted just like the example you provided you can skip this step

I'm gonna use merge sort to sort the data

I changed Tomic-Riedel's mergeSort implementation to match our need.

List<Map<String, int>> mergeSort(List<Map<String, int>> array) {

  // Stop recursion if array contains only one element
  if(array.length <= 1) {
    return array;
  }

  // split in the middle of the array
  int splitIndex = array.length ~/ 2;

  // recursively call merge sort on left and right array
  List<Map<String, int>> leftArray = mergeSort(array.sublist(0, splitIndex));
  List<Map<String, int>> rightArray = mergeSort(array.sublist(splitIndex));

  return merge(leftArray, rightArray);
}


List<Map<String, int>> merge(leftArray, rightArray) {
  List<Map<String, int>> result = [];
  int? i = 0;
  int? j = 0;

  // Search for the smallest eleent in left and right arrays
  // array and insert it into result
  // After the loop only one array has remaining elements
  while(i! < leftArray.length && j! < rightArray.length) {
    if(leftArray[i]['money'] <= rightArray[j]['money']) {
      result.add(leftArray[i]);
      i  ;
    } else {
      result.add(rightArray[j]);
      j  ;
    }
  }

  // Insert remaining elements of left array into result
  // as long as there are remaining elements
  while(i! < leftArray.length) {
    result.add(leftArray[i]);
    i  ;
  }

  // Insert remaining elements of right array into result
  // as long as there are remaining elements
  while(j! < rightArray.length) {
    result.add(rightArray[j]);
    j  ;
  }

  return result;
}

and then we use it to get purchase list code:

final amount = 3500;
final sortedData = mergeSort(data);
// at this point out data is sorted by 'money'

List<Map<String, int>> canBePurchased = [];

for (Map<String, int> item in sortedData) {
  if (item['money']! <= amount) {
    canBePurchased.add(item);
  }
}
print(canBePurchased);

// to get only the last item
if (canBePurchased.isNotEmpty) {
  print(canBePurchased.last);
}
  • Related