✅ Specific Sorting Algorithm
Hey i have been breaking my head about this problem and i have no clue how to solve this.
Im a trainee Developer in Germany and i have been assigned to develop a WebApi and a Website with a Table that needs to show Shipments which are about to be due.
I have a DataModel with different fields like Desired Shipment Date (Type: DateTime), Last Possible Shipment Date, Punctual (Type: Boolean) and various other field but these dont matter for this problem.
So now i have got a unsorted List of these models, which i have to sort after certain criterias:
So they have to be sorted first after their last possible Shipment Date in ascending Order, if that date is already due then the records have to be sorted after the punctual flag with the Shipment Date.
I really dont know how to implement this and looking at least for a lead in the right direction.
48 Replies
You mention a table, is this in a SQL database of sorts? If so, are you using something like EF Core or Dapper to access that database?
its just data from our erp system that i have to put in a table in the web-gui
its just about sorting a List of models after these criterias
Okay, but what datatype is that table?
C# is a strictly typed system
its a List of a custom class containing the attributes Desired Shipment Date (DateTime) and Punctual (Flag). Its just about sorting the elements of the list in place on the mentioned criterias.
ok so a
List<T>
yep
alright so first it sorts by desired shipment date, but if that date has already passed, then what? and do those items go at the front or back of the list?
to the front of list
mhm.
and how does punctual and last possible shipment date come into it?
oh sry mb the last possible is irrelevant. So if the shipment date is already due then all records where the punctual flag is true need to come first then records which are due but have no true punctual flag
ok. easy.
you just need to use
OrderBy
and ThenBy
so it would be ListToSort.OrderBy(x => x.DesiredShipmentDate).ThenBy(x => x.Punctual)
might need a little more then that, but give it a try
ok
this looks sorted according to your rules, right?
sadly no all the records if they are due and have the punctual as true need to come first then the ones which have punctual as false and are due
sry for the misunderstanding
but thats what it does
due and punctual > due and not punctual > not due
thats the order they are in
oh
im dumb
nvm
but how
i tried it like this
so, what is the most important ordering?
ie, what rule must we check first
so if the shipment date is due, then if the punctual flag is true then order the records with the same conditions in a ascending order by the shipment. If the shipment date is not due only by the shipment date in ascending order
we can't really do
if
here
but we both agree that if the item is due or not is the most important thing
so lets start by ordering on that
IsDue is just a local function: bool IsDue(Shipment x) => x.DesiredShipmentDate < DateTime.Now;
ok alright
ok, so now its listing all the items that are due first
great
but it ignores punctual, and the date itself
so, lets make it respect the date. Any ideas on how we would do that?
i would orderBy(x => x.DesiredShipmentDate)
uh oh. Lets follow the advice.
ok, that looks better! but it still doesnt care about punctual
how do we fix that
i would add another .ThenByDescending(x => x.Punctual)
Lets give it a spin
where would you add it, just to be clear?
before the thenBy(x => x.DesiredShipmentDate)
ok
looks good at first, but when the due date isn't passed we have some issues
can you figure out why?
we dont order after the DesiredShipmentDate after the dueDate
no we do, its just that our sort order is pretty well defined
we care about due date, then punctual, then date
so items that are due go first. then punctual, then by date... so, that causes issues
because we dont care about punctual if the item is not due
yep
so, how do we fix it?
as a reminder, this is our current code:
note that I dont have punctual in here
var sorted = list
.OrderByDescending(x => IsDue(x))
.ThenBy(x => x.DesiredShipmentDate)
.ToList();
wait
I'll give you a hint, we don't need to add any more lines. Only modify one of the lines
ok now im lost xD
var sorted = list
.OrderByDescending(x => IsDue(x) && x.Punctual)
.ThenBy(x => x.DesiredShipmentDate)
.ToList();
??
yes!
lol
we only care about punctual if its due
so
isDue(x) && x.Punctual
actually works great
technically, it only works because of the later sorting by date too
since the items that are due but not punctual should come last, and they do because of thisah very nice
you could make it three statements:
due
due and punctual
date
but since the end result of all three rules applied together is the same, I didnt bother with that
yeah before i had a approach where i didnt even check if the date is due because i thought it wouldnt be necessary bc im already sorting after a descending date
and the due one would be at the start to begin with
yep, that was my first approach too
but I quickly realized that I need to know if its due or not since only when its due should
Punctual
matteryep and i missed that point completly
but really great stuff man
really appreciate the help and the educational way u showed it to me
np
here is my full source
If you feel that you're happy and have no further questions, feel free to $close this thread
Use the /close command to mark a forum thread as answered
ok will do