Home > front end >  How to sort a multidimensional array in dart using an inbuilt metheod
How to sort a multidimensional array in dart using an inbuilt metheod

Time:12-26

I have a multidimensional array(list) in a dart that needs to be sorted how can I achieve this in terms of price. Is there any inbuilt method to do this or do I have to do this with code? is it ok to have both a string and an integer datatype in the same list like below

int row = 4;
int col = 4;

var array = List.generate(row, (i) => List(col), growable: false);

//For fill;
array[0][0] = 12;
array[0][1] = "apple";

array[1][0] = 10;
array[1][1] = "drink";

array[2][0] = 15;
array[2][1] = "orange";

array[3][0] = 8;
array[3][1] = "chocolate bar";

array[4][0] = 0;
array[4][1] = "nut";

//I want to sort this array in terms of price 

CodePudding user response:

There is no inbuilt method in dart to do this task. You can try using this package: https://pub.dev/packages/stride

CodePudding user response:

You can do comparison but you have to convert your index type into int. Check below code

array[0][0] = "12";
array[0][1] = "apple";

array[1][0] = "10";
array[1][1] = "drink";

for (var i = 0; i < array.length; i  ) {
    array.sort((a, b) {
      return int.parse(a[0]).compareTo(int.parse(b[0]));
    });
  }
  • Related