sort a list which is a Stream Provider but not changing the order of the actual llist - flutter

I need to sort a list
final products = Provider.of<List<Product>>(context)
I only need to sort it in one screen of more upvotes
products.sort((b, a) => a.voteCount.compareTo(b.voteCount));
doing this made the whole list sorted and i cannot revert back to old format
there is two screens trending list and normal list screen if i sort it then both list look the same i need to only the trending screen to be sorted

You have to create another copy of the list, keep in mind that you can't do the following : List<Product> tmp = products because tmp will reference the same array and this will cause the same issue for you.
Create another array as following : List<Product> tmp = [...products];
and then you can call tmp.sort(...).

Related

Filtering a List using an object field Flutter

I have a List, PointOfServices, that have an object, Orders.
I want to be able to filter the Points of service, based on the orderType field within the Orders object.
The outcome I want is to have a list of Points of service that only have Orders with an orderType of 'inventory'.
Is this possible and if so, how would i go about implementing this?
Check this:
final filtered = pointOfServices.where((pos) =>
pos.orders.where((order) => order.orderType == 'inventory').isNotEmpty).toList();
You are checking if orders list in a single PointOfService contains one order with order type inventory, if such order is present - this serves as a checker for another filter which takes PointOfService to filtered collection

How to get distinct values from object's property with Drift (ex Moor); Flutter

TLDR: "SELECT DISTINCT category FROM items" efficiently in Drift
There is Item (or ItemsCompanion) data class that among others has 'String category' property. I'd like to get a list of distinct categories efficiently.
I'm failing to found a way
to get in result property of object without object itself
to filter out only unique values
Any help would be appreciated.
I can get a list of Items, iterate through and put it's categories in List, transform a List into a Set but it seems way too inefficient.
Using the answer from simolus3 https://stackoverflow.com/a/62359317/8108153
I got this:
Future<List<String>> getItemsCategories() {
final query = selectOnly(items, distinct: true)
..addColumns([items.category])
..where(items.category.isNotNull());
;
return query.map((row) => row.read(items.category)).get();
}
Whether it's the optimal approach or not yet it's way better anything I've come up with

Filter out specific objects of Iterable list by specific property

I have an Iterable list that I am sorting by for different values. I want to add a new filter for users to filter out the list to just show a specific property the list contains. I am trying to use .where, but it does not seem to be working and I am not sure what to do. I have searched the web and see many examples on how to filter out just a plain list for a specific value in the objects with comparable but not able to get this to where for an Iterable list.
Here is what I am trying and not getting any results I would expect.
case 'specificValue':
return sorted.where((t) => t.state == 'speficicValue').toList();
Not sure if .where is the right way to filter out an Iterable list by a specific value within the objects of the list.
This was an issue with my new experience to Flutter and not knowing how too print. The list being filtered with .where was working but the list was so large it was hard to make sure it was showing up how I wanted. So printing it to the terminal I was able to look at the objects easier to validate the list was being filtered as I expected.

Take an Element of a List which is in a List

I know this sounds confusing. I have a List which consists of Lists in these Lists i have always 2 Elements. I want to display the first Element of all Lists. How does it work?
When you trying to display the list of elements as a group using foreach() loop. By default the list will be displayed in Ascending order only, try that approach once. If you face any issue, please provide your source code, as you trying out to display group of sublists in a Listview. Happy coding

xamarin forms distinct objects sort based on the no of times that they appear

I have made an Xamarin forms application and the problem that I have a list named lista2 which has an object named poli. Poli is the city that it is located. I want to make a list based on distinct cities, which i have done using this command
var poleislist = lista2.Select(x => x.poli).Distinct();
now I want this poleislist to be sorted based on the amount of times that each element is on the lista2. For example the bigger cities appear more often and I want them in the first places of the list because this list(poleislist) is going to be the itemsource of a picker.
Thank you very much!
Instead of using Distinct you could Group the list, sort it by count descending then Select the poli.
It would be something like this:
var poleislist = lista2.GroupBy(item => item.poli)
.OrderByDescending(a => a.Count())
.Select(x => x.Key)
.ToList();
Note: sorry did not get to test it but this should work. Basically this should get you all the cities in the list, ordered descending by the number of times it appears.
Hope this helps.-