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 9291097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:34:10+00:00 2026-06-18T20:34:10+00:00

What would be a good (simple) algorithm/method for this sorting situation: I have an

  • 0

What would be a good (simple) algorithm/method for this sorting situation:

I have an array of items where each item consists of 2 fields: (ID, Timestamp)

There are many pairs of items with the same ID’s.

I want to sort the array such that the items are alternating among ID’s as much as possible, starting from the item with the earliest Timestamp.

For example, with this input:

(1, 15:00)
(1, 15:05)
(1, 15:10)
(2, 15:15)
(2, 15:20)
(2, 15:25)
(3, 15:30)
(4, 15:35)
(3, 15:40)

I would get this output:

(1, 15:00)
(2, 15:15)
(3, 15:30)
(4, 15:35)
(1, 15:05)
(2, 15:20)
(3, 15:40)
(1, 15:10)
(2, 15:25)

I’m primarily looking for a algorithm which is conceptually simple, but of course it’s nice if it’s performant.

Right now the best I can think of is something like:

  1. Init/create temporary array T
  2. From the array A: Get item X with earliest Timestamp where ID is not in T
  3. Add X to result set R, add X.ID to T, pop X from A
  4. As long as X exists, repeat step 2-3, otherwise restart at step 1
  5. Quit when A is empty
  • 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-06-18T20:34:11+00:00Added an answer on June 18, 2026 at 8:34 pm
    1. Put all items in a sorted multimap that sorts first by ID, then by time. (For instance you can implement this with your favorite sorted associative container keyed by ID, where each value in the sorted container is itself another sorted container that holds all time values.
    2. While there are still any elements in this multimap: for each key in ascending order, remove the first element with that key and add it to the result.

    When you are done, if I’ve understood the problem correctly, the result will be in the order you want.

    Here’s an example in Java, using Guava TreeMultimaps (untested):

    TreeMultimap<Id, Timestamp> map = new TreeMultimap<Id, Timestamp>();
    for (/* ... every item ... */) {
      map.put(item.getId(), item.getTimestamp());
    }
    
    List<Entry> outputList = new ArrayList<Entry>();
    while (!map.isEmpty()) {
      for (Id key : map.keySet()) {
        Timestamp value = map.get(key).first();
        outputList.add(new Entry(key, value));
        map.remove(key, value);
      }
    }
    return outputList;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some useful wpf buttons to test some functionality. It would be good
Two good examples would be google and facebook . I have lately pondered the
Before I throw something ugly together I thought this would be a good one
I have a project in mind whose main selling point would be very good
I am having a general problem finding a good algorithm for generating each possible
i am implementing the simple bubble sort algorithm using CUDA, and i have a
i would like to implement a good throttle algorithm by in .net (C# or
I've decided that it would be good for me to move outside of my
What would be a good way to determine if a string contains an IPv4
What would be a good way to writein C# a .NET3.5 Thread Safe DataSource

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.