## linq – Make a method that uses Expression> for filtering and sorting more flexible

I have a method in a service that performs a Linq-to-SQL query:

``````public async Task<IEnumerable<Incident>> GetIncidentsAsync<TOrderBy>(
Expression<Func<Incident, bool>> whereExpression,
Expression<Func<Incident, TOrderBy>> orderByExpression
)
{
return await (
from i in _context.Incidents
// inner join
.Include(i => i.IncidentType)
.Where(whereExpression)
.OrderBy(orderByExpression)
// left-outer join
join ft in _context.Driver on i.FaultTypeId equals ft.Id into ift
from faultTypes in ift.DefaultIfEmpty()
// left-outer join
join v in _context.Vehicles on i.VehicleSerialNo equals v.VehicleSerialNo into iv
from vehicles in iv.DefaultIfEmpty()
select new Incident
{
Id = i.Id,
IncidentTypeId = i.IncidentTypeId,
IncidentType = i.IncidentType,
// datetimes
Occurred = i.Occurred,
Reported = i.Reported,
Resolved = i.Resolved,
//
VehicleSerialNo = i.VehicleSerialNo,
Vehicle = vehicles,
//
DriverSerialNo = i.DriverSerialNo,
Driver = drivers
}
).ToListAsync();
}
``````

Improvements:

• having to manually compose the `Incident` to be returned feels like an anti-pattern
• handle situations where the `whereExpression` is null; this will try an exception now
• handle situations where the `orderByExpression` is null; this will try an exception now
• change the sorting direction from ascending to descending by changing the `orderByExpression` (if this is possible)
• is there value to marking the method as `<TOrderBy>`?

## Problems when sorting a Google sheet that someone else is editing

In the past, when two people were editing the same Google sheet, if person A attempted to do a sort they would get a “heads-up” warning that A’s sort was going to affect person B.

We now don’t seem to get that heads-up message anymore, but we frequently have cases where if person A sorts while person B is editing a cell, the edits go into the wrong cell … i.e., they go into the physical row and column that B was initially editing, but because of the sort, this is no longer the correct destination for the edits.

## 2013 – How to incorporate sorting in calculated column with background and text color formatting to use as a Grouping field

Here is the calculated code for the field:

=IF((AU-R CIR)=”7. ERROR”,”<div style=”””&”text-align:center; color: red; background: #ffffff”&”””>7. ERROR”,IF((AU-R CIR)=”6. Incomplete”,”<div style=”””&”text-align:center; color: blue; background: #ffffff”&”””>6. Incomplete”,IF((AU-R CIR)=”5. High”,”<div style=”””&”text-align:center; color: white; background: #c00000″&”””>5. High”,IF((AU-R CIR)=”4. Moderate-High”,”<div style=”””&”text-align:center; color: white; background: #ed7d31″&”””>4. Moderate-High”,IF((AU-R CIR)=”3. Moderate”,”<div style=”””&”text-align:center; color: white; background: #ffc000″&”””>3. Moderate”,IF((AU-R CIR)=”2. Low-Moderate”,”<div style=”””&”text-align:center; color: white; background: #92d050″&”””>2. Low-Moderate”,IF((AU-R CIR)=”1. Low”,”<div style=”””&”text-align:center; color: white; background: #00b050″&”””>1. Low”)))))))

I want to use as a grouping field but the built in ordering for grouping fields does not order based on the (AU-R CIR) field values. Can can the calculation code be adjusted to accommodate the ordering?

## Platform Identifier – Identify & Sorting Done QUICK!

Hello!
YOU tell the software what to use to identify using both a powerful inUrl and inHtml method.
Webpage: https://www.platform-identifier.xyz/
Best Regards

## Auto sorting multiple column

Can we auto sort multiple column in google sheet without using script?
I can’t open the script so I can’t try the proposed script here

## Sorting by “Best selling” not working for configurable products M2.4.x (but for simple products)

the sorting by “Best selling” is not working for configurable products in M2.4.x

Its well working for categories with simple products.

When mixed category, first the best-selling simple products are shown, after that all the configurable products (in wrong order).

We use the plug-in from weltpixel. According weltpixel, their plugin cannot be the reason.

The whole thing was working till oct 2020 approx, it could be after the update from M2.3 to M2.4.1 ist stopped working but i am not sure..

Any ideas???

## What kind of card sorting is the best?

I have an online grocery website, it includes different categories and subcategories, I want to test the the usability of categories tree, in this case closed card sorting or open card sorting is the most suitable? and how many people should participate ?

## sorting – sort Teams list alphabetically in MS Teams?

I have a whole bunch of Teams that I need to look at on a regular basis in MS Teams, and I’d like to organize them by sorting them alphabetically. Sorting things alphabetically may seem passé in a time where drag and drop sorting is the thing to do, but it sure is nice not to have to actively decide where to put things. Is there a way to do this other than manually? I can’t find anything, and this seems like a really dumb feature to omit so my assumption is that I’m probably just missing something.

## sorting – ordered set inclusion

Suppose I have a ordered set $$A={2,3}$$ and $$B={2,3,4}$$ and $$C={2,4,3}$$.
Is there any mathematica command whether $$Asubset B$$ gives true(or 1) but $$A$$ which is not a subset of $$C$$ gives false(or 0)?

I know orderedQ, but ordered Q only gives $$OrderedQ({2,3})=true$$ and $$OrderedQ(3,2)=False$$, so it was not that useful.

## time complexity – Algorithms for almost sorting

Call an array $$C$$-sorted if each element is at-most $$C$$ places away from its place had the array been sorted (this is the same as “almost”-sorted array in the question, but in plain English).

Assume we will use comparison sort.

Claim One: Given an array of $$n$$ elements that is $$C$$-sorted, we can sort it with at most $$nlceillog_2 (C+1)rceil$$ comparisons.

Proof: Here is how we can sort the given array.

1. Create a buffer that is an array of size $$C+1$$.

2. One by one move the first $$min(n, C+1)$$ elements to the buffer, keeping the buffer sorted using binary search to find the right place for each new element.

3. One by one move the min element from the buffer array to the result array, and, if the given array is not empty yet, add the next element to the buffer, keeping the buffer sorted using binary search to find the right place for that next element.

For each element in the array, at most $$lceillog_2 (C+1)rceil$$ comparisons is used to find its right place in the buffer. The number of total comparisons used is at most $$nlceillog_2 (C+1)rceil$$. $$quadcheckmark$$

Claim Two: At least $$log_2 (n!) – nlceillog_2 (C+1)rceil$$ comparisons are needed to $$C$$-sort an array of size $$n$$ in the worst case.

Proof: Otherwise, suppose there is an algorithm that can $$C$$-sort any given array of size $$n$$ with less than $$log_2 (n!) – nlceillog_2 (C+1)rceil$$ comparisons. Extending that algorithm with the algorithm shown in the proof of claim one, we obtain an algorithm that can sort any given array of size $$n$$ with less than $$log_2(n!)$$ comparisons. That is well-known to be impossible. $$quadcheckmark$$

Claim Three: There is no algorithm that can $$C$$-sort an array of size $$n$$ with $$O(frac{nlog_2 n}{C})$$ comparisons.

Proof: We have, if $$nge C^{1+epsilon}$$ for some fixed $$epsilongt0$$,
begin{aligned}lim_{Ctoinfty} frac{log_2 (n!) – nlceillog_2 (C+1)rceil}{frac{nlog_2 n}{C}} &= lim_{Ctoinfty} Cleft(frac{log_2 (n!)}{nlog_2 n}-frac{lceillog_2 (C+1)rceil}{log_2 n}right)\&ge lim_{Ctoinfty} Cleft(1-frac1{1+epsilon}right)=infty. end{aligned}quadcheckmark

Indeed, $$log_2 (n!) – nlceillog_2 (C+1)rceil$$ given a pretty strong lower bound for the number of comparison needed in the worst case since
$$log_2 (n!)$$ is the lower bound to ordinarily sort an array, and, as $$log_2(n!)approx nlog n$$, for any fixed $$C$$,

$$nlceillog_2 (C+1)rceil = o(log_2 (n!)). color{#d0d0d0}{text{(small o, not big O!)}}$$

That is, at least $$(1-epsilon)log_2(n!)$$ comparison is needed to $$C$$-sort array when $$n$$ is sufficiently large, for all fixed $$epsilongt0$$ and $$C$$.

We also note that

on average, quicksort performs only about 39% worse than in its best case. In this sense, it is closer to the best case than the worst case. A comparison sort cannot use less than $$log_2(n!)$$ comparisons on average to sort $$n$$ items (as explained in the article Comparison sort) and in case of large $$n$$, Stirling’s approximation yields $$log_2(n!) approx n(log_2 n − log_2 e)$$, so quicksort is not much worse than an ideal comparison sort.

Now let us have an array of $$n=1000$$ elements and $$C=10$$.

• Quicksort will perform about $$2nlog napprox 19931$$ comparisons on average to sort the array, as shown on Wikipedia.
• Claim Two says $$log_2 (n!) – nlceillog_2 (C+1)rceilapprox4529$$ are needed to $$10$$-sort the array in the worse case, which is sort of information-theoretical the best.
• Note that $$frac{nlog_2 n}{C}approx997$$, which is much less than
of 4529.

So I believe, (some more argument are omitted here), an algorithm that performs about $$nlog_2(n/C)approx6643$$ comparisons to $$10$$-sort the array is pretty much about the best we can get.