I am trying to sort a 2D list depending on user requirement like [i][1] or [i][2]. To achieve this I first convert the list to a map and then add a map to the list then sort list containing map and then again convert it to a 2D list (I know sounds a bit stupid but this was the best solution I found). Code -
{
List a = [];
List a1 = [];
//Convert 2D list to list Map
for (int i = 0; i < itemData.length; i++) {
Map<String, dynamic> b = {
"0": itemData[i][0],
"1": itemData[i][1],
"2": itemData[i][2],
"3": itemData[i][3],
"4": itemData[i][4],
"5": itemData[i][5],
"6": itemData[i][6],
"7": itemData[i][7],
"8": itemData[i][8],
};
a.add(b);
}
// sorting list map
a.sort((m1, m2) {
var r = m1[x].compareTo(m2[x]);
return r;
});
// reconverting list map to 2D list
for (int i = 0; i < a.length; i++) {
a1.add(a[i].entries.map((e) => e.value).toList());
}
}
The code works fine for 6 elements but not sure why it's not working for more elements. when I am printing a after creating a map and adding to the list its printing only 6 elements. I don't know what's going wrong why it's failing to add all elements, if there is a better way i can do this or whats wrong i am doing in it.
Eg
[[18,1,2.3,true],[18,10,8.5,true],[18,2,6.5,false],[24,5,7.6,true],[12,2,3.4,false]]
sorting it depending on [i][2] (third element of each list)
[[18,1,2.3,true],[12,2,3.4,false],[18,2,6.5,false],[24,5,7.6,true],[18,10,8.5,true]]