Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6070297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:55:01+00:00 2026-05-23T09:55:01+00:00

I was looking for an algorithm for sorting an array in a custom way

  • 0

I was looking for an algorithm for sorting an array in a custom way but I didn’t succeed in finding the proper solution to my problem. I’ll describe the code in Django-like syntax but it’s not necessary to limit a solution only for Django.

Let’s suppose I have the following models (classes):

class Website(models.Model):
    ...
class Offer(models.Model):
    website = models.ForeignKey(Website, on_delete=models.CASCADE)
    ...

And let’s suppose I have the following instances:

  • Offer 1 -> Website A
  • Offer 2 -> Website B
  • Offer 3 -> Website B
  • Offer 4 -> Website B
  • Offer 5 -> Website C
  • Offer 6 -> Website A
  • Offer 7 -> Website A
  • Offer 8 -> Website C

This instances form a sequence (array):

sequence = [Offer 1, Offer 2, Offer 3, Offer 4, Offer 5, Offer 6, Offer 7, Offer 8]

I need to sort the sequence in the way where Offers with the same Website cannot stand one after another nevertheless the original order should stay as same as possible.

So the sorted sequence should look this way:

sequence = [Offer 1, Offer 2, Offer 5, Offer 3, Offer 6, Offer 4, Offer 7, Offer 8]

Positive Examples:

  • Website A, Website B, Website A, Website C, Website A
  • Website A, Website B, Website C, Website B, Website C
  • Website A, Website B, Website A, Website B, Website A

Negative Examples:

  • Website A, Website B, Website B, Website A, Website B, …
  • Website B, Website C, Website A, Website A, Website B, …
  • Website B, Website C, Website A, Website C, Website C, …

Thanks for any suggestion.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:55:02+00:00Added an answer on May 23, 2026 at 9:55 am

    This should work:

    def gen_best_order(orig):
        last = None
        while len(orig) > 0:
            deli = None
            for i, m in enumerate(orig):
                if m.website != last.website:
                    last = m
                    deli = i
                    yield m
                    break
            if deli is None:
                last = orig[0]
                yield orig[0]
                deli = 0
            del orig[deli]
    ordered = list(gen_best_order(sequence))
    

    This is a generator that will try and yield elements in order, but if the next element equals the last element yielded, it will skip it. If it gets to the end of the list and there is no way to yield something that doesn’t equal the previous, it just yields it anyway.

    Here’s an example of it working on a list of numbers:

    def gen_best_order(orig):
        last = None
        while len(orig) > 0:
            deli = None
            for i, m in enumerate(orig):
                if m != last:
                    last = m
                    deli = i
                    yield m
                    break
            if deli is None:
                last = orig[0]
                yield orig[0]
                deli = 0
            del orig[deli]
    
    nums = [1,2,3,3,4,5,5]        
    print 'orig:', nums
    print 'reordered:', list(gen_best_order(nums))
    

    This prints:

    orig: [1, 2, 3, 3, 4, 5, 5]
    reordered: [1, 2, 3, 4, 3, 5, 5]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for an algorithm (or PHP code, I suppose) to end up with
I'm looking for an algorithm that sorts strings similar to the way files (and
I'm looking to sort an array of about 200-300 objects, sorting on a specific
I'm looking for an algorithm to calculate ln(1-x). x is often small (<0.01), but
I'm looking to quicksort some objects in php. i'm sorting an array of OBJECTS
I was looking for an algorithm, so OS is not problem, in how to
I'm not looking for an implementation, just pseudo-code, or at least an algorithm to
Im looking for an algorithm to be used in a racing game Im making.
I'm looking for an algorithm to detect if two rectangles intersect (one at an
I am looking for an algorithm to calculate the next set of operations in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.