I have data like this
[{"name": "swimming"},{"name": "Politics"},{"name": "Gamer"}]
and I have profiles like
[
{
"username":"abc",
"passions":[
{
"name":"Snooker"
}
]
},
{
"username":"abc",
"passions":[
{
"name":"Coding"
}
]
},
{
"username":"xyz",
"passions":[
{
"name":"swimming"
}
]
},
{
"username":"abc",
"passions":[
{
"name":"Politics"
},
{
"name":"swimming"
}
]
}
]
What I need to do is show first those profiles whose passions are matching with that first data array and then the other rest remaining will show.
CodePudding user response:
I'm sure there are better ways to do this, but this works.
final passions = [{"name": "swimming"},{"name": "Politics"},{"name": "Gamer"}];
final users =
[
{
"username":"abc",
"passions":[
{
"name":"Snooker"
}
]
},
{
"username":"efg",
"passions":[
{
"name":"Coding"
}
]
},
{
"username":"hij",
"passions":[
{
"name":"swimming"
}
]
},
{
"username":"klm",
"passions":[
{
"name":"Politics"
},
{
"name":"swimming"
}
]
}
];
var matches = [];
users.forEach((u) {
passions.forEach((p) {
if (p['name'] == (u['passions'] as List)[0]['name']) {
matches.add(u['username']);
}
});
});
print(matches.toString());
CodePudding user response:
Just give this function a profile list and Finally returns the sort list
List passions = [{"name": "swimming"}, {"name": "Politics"}, {"name": "Gamer"} ];
List sortListPrfile(List profiles) {
//profiles is a list of result server
List _items = []; // It will be returned eventually
List _noItemCommon = []; //Items that are not common
for (var profile in profiles) {
for (var passion in passions) {
if (profile["passions"][0]["name"] == passion['name']) {
_items.add(profile);
} else {
_noItemCommon.add(profile);
}
}
}
_items.addAll(_noItemCommon);
return _items;
}