Reputation: 165
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:
Upvotes: 1
Views: 98
Reputation: 14035
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 TreeMultimap
s (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;
Upvotes: 2