Order list of objects by custom order initialized in other list - list

i have list a of objects which type is Person :
class Person{
int id
String name
String bestColor
}
def persons = [ new Person(1,'Abdennour','white'),
new Person(2,'Ali','red'),
new Person(3,'Hsen','white'),
new Person(4,'Aicha','green') ]
and i have a list of color:
def colors=['green','white','red']
i want to order persons list according to 3rd field(bestColor) . However, I do not want alphabetical order of colors , but rather I want the same order as colors list.
that it means , the expected result is :
def persons=[new Person(4,'Aicha','green')
,new Person(1,'Abdennour','white')
,new Person(3,'Hsen','white')
,new Person(2,'Ali','red')]

So given:
#groovy.transform.Canonical
class Person{
int id
String name
String bestColor
}
def persons = [ new Person( 1, 'Abdennour', 'white' ),
new Person( 2, 'Ali', 'red' ),
new Person( 3, 'Hsen', 'white' ),
new Person( 4, 'Aicha', 'green' ) ]
def colors = [ 'green','white','red' ]
You can just do:
// Sort (mutating the persons list)
persons.sort { colors.indexOf( it.bestColor ) }
// check it's as expected
assert persons.id == [ 4, 1, 3, 2 ]

Related

When Modify a Value in the Copied List, It Modify the Value in The Original List Flutter

I want to copy list and make no reference even when I edit value in copied list. I tried addAll(), List.from(), map(), toList() and [...myList] but they were unhelpful.
Edit for Clarification
Ex:
class Item{
String description;
int price;
Item(this.description, this.price);
}
List<Item> items = [
Item('item 1', 100),
Item('item 2', 200),
Item('item 3', 300),
];
List<Item> selectedItems = List.from(items);
When I edit selectedItems original list items shouldn't be affected, but it's not?!
selectedItems[0].price = 115;
it modifies the price of element 0 in both.
Flutter haven't copy function for objects, you should create "copy constructor" manually. It's discussed here: How can I clone an Object (deep copy) in Dart?
Solution for your example:
void main() {
// Original list
List<Item> items = [
Item('item 1', 100),
Item('item 2', 200),
Item('item 3', 300),
];
// Copied list
List<Item> selectedItems = items.map((it) => Item.copy(it)).toList();
// Change price for item in copied list
selectedItems[0].price = 115;
print("Original list:");
items.forEach((it) => print("Description: ${it.description}, price: ${it.price}"));
print("Copied list:");
selectedItems.forEach((it) => print("Description: ${it.description}, price: ${it.price}"));
}
class Item {
String description;
int price;
Item(this.description, this.price);
// Copied constructor
Item.copy(Item item) : this(item.description, item.price);
}

How to count category value of array of List

let's say I have a list like the example below
<Categories>myList = [
Categories(
nameCategory: 'Book',
amount: '20'
),
Categories(
nameCategory: 'Book',
amount: '40'
),
Categories(
nameCategory: 'Food',
amount: '20'
),
Categories(
nameCategory: 'Food',
amount: '15'
),
];
How I can combine the duplicate values of that list and count the value of the list based on name ??
I can combine the list and the count value of the list but that only works just in a general list like sum total
what I want to do is make a new List but only combine several parts that share the same property like the same category or same class like that
this is an example what I want to achieve
<Categories> anotherList= [
Categories(
nameCategory: 'Book',
amount: '60'
),
Categories(
nameCategory: 'Food',
amount: '35'
),
];
I would replace your List<Categories> with a Map<String, Categories>. Then you can easily look up the Categories object given its name and mutate the existing Categories object. For example, something like:
var mergedCategories = <String, Categories>{};
for (var categories in myList) {
var name = categories.nameCategory;
var amount = categories.amount;
(mergedCategories[name] ??= Categories(nameCategory: name, amount: 0))
.amount += amount;
}
You're essentially trying to get an aggregate value from a list, which is what List.fold is meant to help with.
Here's an example of how you might use it:
class Category {
final String name;
int amount;
Category({required this.name, required this.amount});
String toString() => "Category(name: $name, amount: $amount)";
}
void main() {
final categories = [
Category(
name: 'Book',
amount: 20
),
Category(
name: 'Book',
amount: 40
),
Category(
name: 'Food',
amount: 20
),
Category(
name: 'Food',
amount: 15
),
];
/**
* Here is where the aggregation is done
*/
final List<Category> aggregated = categories.fold([], (list, item) {
try {
// Check whether the category is already in the aggregate
final existingCategory = list.firstWhere((c) => c.name == item.name);
// Category is already in the list, so just add the amount of the current item.
existingCategory.amount += item.amount;
return list;
} catch (_) {
// The category has not yet been added - so add it here
list.add(item);
return list;
}
});
print(aggregated);
}
I've changed your category class a bit for simplicity, but the principle should be the same. You can read more about the fold function here: https://api.dart.dev/stable/2.13.4/dart-core/Iterable/fold.html
A pretty straightforward method is by using the groupBy function provided by the collection.dart package.
import 'package:collection/collection.dart';
groupBy<Categories, String>(list, (c) => c.nameCategory).values.map(
(list) => list.reduce(
(a, b) => new Categories(a.nameCategory, a.amount + b.amount)
)
);

how to get value of item in array of list and make them independent list

I have list like example below, I want to get value of nameCategory and make them to List
List<Categories> iconCategory= [
Categories(
iconPath: 'assets/images/icons/IncomeIcon/001-salary.svg',
nameCategory: 'Salary',
),
Categories(
iconPath: 'assets/images/icons/IncomeIcon/002-interest.svg',
nameCategory: 'Interest',
),
Categories(
iconPath: 'assets/images/icons/IncomeIcon/003-award.svg',
nameCategory: 'Award',
)];
to list like example below but the data is from array list above
how I can do that
List<String> myCategory = ['Salary','Interest','Award'];
You can simply use the map method to achieve this:
List<String> myCategory = mycategories.map((e) => e.nameCategory).toList();
print(myCategory); // Prints [Salary, Interest, Award]
this will give you what you need
List<Categories> s1 = [];
List<String> ss = [];
result!.map((e) {
ss.add(e.nameCategory);
});

Dart/Flutter - Compare two List<dynamic> if they have the same value of id

I have two List<dynamic> and I am trying to figure out how I can check if there is a same value in the id field
List list1 = [
{"id": 2, "name": "test1"},
{"id": 3, "name": "test3"}
];
List list2 = [
{"id": 2, "name": "test1"}
];
I tried this but it returns me false
var isMatch = (list1.toSet().intersection(list2.toSet()).length > 0);
You can not compare like that because you can't compare dynamic as Boken said, you need to create a class for your object and implement a basic search , you can convert list2 into a set to make the search less complex (contains function)
void main() {
List list1 = [
MyObject(2,"test"),
MyObject(3,"test1")
];
List list2 = [
MyObject(4,"test")
];
for(int i=0;i<list1.length;i++){
if(list2.contains(list1[i])){
// do your logic
print(true);
break;
}
}
}
class MyObject{
int id;
String name;
MyObject(int id,String name){
this.id = id;
this.name = name;
}
// redifine == operator
bool operator ==(o) => (o as MyObject).id == this.id;
}

Dapper nested list query

I have 3 POCO classes as below;
Continent has many ContinentPart
Continent has many Country
Country has many City
I want to get Continens with ContinentParts, Countries and Cities
using (IDbConnection db = new SqlConnection(_conf["ConnectionStrings:WorkConStr"]))
{
string query = #"SELECT * FROM Continent as c
LEFT JOIN ContinentPart as cp ON c.ContinentId=cp.ContinentId
LEFT JOIN Country as co ON c.ContinentId=co.ContinentId
LEFT JOIN City ci ON co.CountryId=ci.CountryId
ORDER BY c.RecDate DESC";
var continentDictionary = new Dictionary<int, Continent>();
var list = db.Query<Continent, ContinentPart, Country, City, Continent>(
query,
map: (continent, continentPart, country, city) =>
{
Continent m;
if (!continentDictionary.TryGetValue(continent.ContinentId, out m))
{
continentDictionary.Add(continent.ContinentId, m = continent);
}
if (m.ContinentParts == null)
m.ContinentParts = new List<ContinentPart>();
m.ContinentParts.Add(continentPart);
if (m.Countries == null)
m.Countries = new List<Country>();
m.Countries.Add(country);
foreach (var h in m.Countries)
{
if (h.Cities == null)
h.Cities = new List<City>();
h.Cities.Add(city);
}
return m;
},
param: new { },
splitOn: "")
.Distinct()
.ToList();
return list;
}
You have two options here. You can use Multiple Resultsets feature:
https://medium.com/dapper-net/handling-multiple-resultsets-4b108a8c5172
Or, another option, better in my opinion, is to return the joined result as a JSON, for example with a query like the following:
select
continents.continent_id,
continents.continent,
countries.country_id,
countries.country,
cities.city_id,
cities.city
from
dbo.Continent continents
inner join
dbo.Country countries on continents.continent_id = countries.continent_id
inner join
dbo.City cities on countries.country_id = cities.country_id
for
json auto
that returns a JSON like this:
{
"continent_id": 1,
"continent": "Africa",
"countries":
[
{
"country_id": 1,
"country": "Nigeria",
"cities":
[
{
"city_id": 1,
"city": "Lagos"
}, {
"city_id": 2,
"city": "Abuja"
}
]
}
]
}
and that can then be turned into a complex object using Custom Type Handling:
https://medium.com/dapper-net/custom-type-handling-4b447b97c620
Links are to articles I've wrote on the subject.