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

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.-

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

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

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(...).

Sphinx Filtering based on categories using OR

I have the following text fields I search with Sphinx: Title, Description, keywords.
However, sometimes things are narrowed down using categories. We have 3 category fields: CatID1, CatID2 and CatID3.
So, for example, I need to see if the word "Kittens" is in the Title, Description, or Keywords, but I also want to filter so that only items that have the categories (Animals - ID Number 8) or (Pets - ID Number 9) or (Felines - Category ID Number 10) in either of those CatID fields.
To clarify, only show items that have a 8,9 or 10 in CatID1, 2 or 3.
Any ideas on how I would accomplish this using sphinx filtering or searching the CatID1 fields as keywords?
Note: I am able to filter and it works great only using one category, i.e:
if(!empty($cat_str)) {
$cl->SetFilter( 'catid1', array( $cat_str ));
}
Thanks!
Craig
SetFilter takes an array. In your example you are putting $cat_str into an array. A array of one item.
So you just needs to build array with all the ids.
$cl->SetFilter( 'catid', array( $cat1, $cat2, $cat3 ));
But thats not very flexible. So you probably build the array dynamically, rather than hard-coded like that. But thats upto your application how to build the array.
But also storing the ids, in three sperate attributes, makes it hard to search. Notice in the above example, just noticed a attribute called catid. This would be a single multi-value attribute, that contains the ids from all three cat fields. That way its easy to search for ids in ANY of the columns at once.
http://sphinxsearch.com/docs/current.html#mva
if using a sql source, could do with something like
sql_query = SELECT id, title ... , CONCAT_WS(',', CatID1, CatID2 and CatID3) as catid FROM ...
sql_attr_multi = uint catid from field;

Linq to Entities query to select latest, distinct records

I'm trying to retrieve records ordered by a DateTime with Linq to entities, but I need them as distinct records as well.
My table design looks like this:
Where blogItemNodeId is an Umbraco CMS node (the blog item node to which a comment can be created)
The requirement is that a list of blog items can be sorted by how many comments they have. So I need to get the latest comments distinct by blogItemNodeId to display those blog items.
My current linq query looks like this:
var distinctComments = (from c in ctx.BlogComments
orderby c.date
group c by c.blogItemNodeId
into uniqueRecords
select uniqueRecords.FirstOrDefault());
The problem with this query is that it finds the first (hence FirstOrDefault()) record with distinct blogItemNodeId where it should find the one which is newest of all comments with the same blogItemNodeId.
Does anyone know how to achieve this? :-)
Thanks a lot in advance.
EDIT
I managed to get it to work by doing this:
var allComments = (from c in ctx.BlogComments
orderby c.date descending
select c).ToList();
var distinctComments = allComments.GroupBy(x => x.blogItemNodeId).Select(y => y.FirstOrDefault());
But then I have to get all the comments before doing the group which is not very elegant nor performant.
Any help is greatly appreciated!
You could combine both the orderBy statement and the GroupBy statement into one single statement like this:
var distinctComments = ctx.BlogComments.OrderBy(c => c.date)
.GroupBy(x => x.blogItemNodeId)
.Select(y => y.FirstOrDefault());
If the performance is bad, you could cache your DataContext in the HttpCache through a Singleton. See this blogpost for an example. This is based on the Umbraco DataContext but can be easily modified for use with another type of context.