A sort algorithm?

23 views
Skip to first unread message

MikeKJ

unread,
Feb 17, 2017, 4:43:12 AM2/17/17
to Django users
Anyone figure out a sort algorithm to do this please?

list = DateTimeDuration.objects.all().filter(date=now).order_by('resource','start')

max_hours = 8

this produces

Resource – start – duration – user-name

 

Item - 9 - 4 - T Test
Item- 10 - 4 - Alpha Bravo
Item - 14 - 2 - Herod First


what I need is to sort it so that it outputs like this:


Item - 9 - 4 - T Test

Item - 14 - 2 - Herod First

Item - 10 - 4 - Alpha Bravo


The parameters are:


There are 8 hours in a day (max_hours = 8)


the list of objects  need to be sorted in such a way that if 2 or more objects combined duration is less than 8 then they need to be sorted in order of start time but if the start time of another object

is the same or the start time is within the duration of an object then it must be shuffled backwards


As an adder:


Each resource has a finite number so is  there a way to utilise the max number of resources within a resource so that we know how many resources are unused?


MODEL for info


class Resource(models.Model):
    resource = models.CharField(max_length=30)
    resource_capacity = models.IntegerField()
    seating = models.IntegerField()

    def __unicode__(self):
        return self.resource

class User(models.Model):
    name = models.CharField(max_length=200)
    email = models.CharField(max_length=100, null=True, blank=True)
    tel = models.CharField(max_length=100, null=True, blank=True)


class DateTimeDuration(models.Model):
    user = models.ForeignKey(User)
    resource = models.ForeignKey(Resource)
    date = models.DateField()
    start = models.IntegerField(max_length=5, help_text="whole number like 9, 10, 11, 12, 13, 14, 15, 16, 17")
    hours = models.IntegerField(max_length=2, help_text="Number of hours as a whole number like 3")

    class Meta:
        verbose_name_plural = 'Booking Date-Time-Duration'
        ordering = ('date',)


Anyone point me in the right direction please?



Melvyn Sopacua

unread,
Feb 17, 2017, 9:24:03 AM2/17/17
to django...@googlegroups.com

On Friday 17 February 2017 01:43:12 MikeKJ wrote:

 

> As an adder:

>

>

> Each resource has a finite number so is there a way to utilise the

> max number of resources within a resource so that we know how many

> resources are unused?

 

Hmm, I'd rethink my datamodel. Everything is far easier to handle if a resource is a unique thing that can't be booked by two users at the same time.

 

This also affects sorting. It is easy if there's only one overlap, but things get tricky, when there's more than one, cause you'd have to wait for the booking to show up that isn't overlapping.

 

It's also not very clear what you're trying to show by sorting it that way and that is probably easier to do with a better data model.

 

Let's say you have 10 bedrooms. Users book a "bedroom" and you allocate one from the available bedrooms for that timeslot. If none are available, you deny booking. For each hour of the day, it is trivial to calculate the number of rooms unused.

You filter out target date, break down start + hours in start times for each hour, build two sets for target slot and room slot and see if they intersect:

 

Room 1 for today:

start: 9

hours: 4

becomes

set(9, 10, 11, 12)

 

Room 2 for today:

start 11:

hours: 2

becomes:

set(11, 12)

 

Room 3 for today:

start: 12

hours: 4

becomes

set(12, 13, 14, 15)

 

Target:

start: 11

becomes

set(11)

 

Room 1 and 2 intersect with target. Room 3 is needed at 12 because both 1 & 2 are still occupied.

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages