-3

I need to order a list of custom object based on multiple criteria:

My object is like this:

public class MyJob {
   private Date finish;
   private int  time;
   private int  param1;
   private int  param2;
   private int  param3;
   private int  size;
   //...
}

What I need is to create an algorithm to sort an array of there jobs based on:

  1. A job cannot finish after the "finish" attribute
  2. Given a job at index i, job at position i+1 must have one of these pattern:
    • The attribute param1 need to be one more or less than the param1 of the sibling job and param2 and param3 are equal
    • attribute param2 need to be one more or less than the param2 of the sibling job and param1 and param3 are equal
    • attribute param3 need to be one more or less than the param3 of the sibling job and param1 and param2 are equal.
  3. After two days, algorithm can start with any kind of param1, param2 or param3.
  4. If a job has a size more than a size given to the method, it has to be put at the end of the list.
  5. Optional, the algorithm can start from param1 or param2 or param3 passed as arguments.

Can someone give me the right direction to implement these multiple sort algorithm?

I think about some kind of selection sort edited but I can't start.

JNYRanger
  • 6,829
  • 12
  • 53
  • 81
  • Take a look at the question referred to be djechlin, even though the criteria are different it will still explain how to sort based on multiple criteria. – Captain Man May 26 '15 at 19:04

1 Answers1

0

Write your own custom Comparator implementation.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • Merely have a `Comparator` isn't enough, you need to sort them with it too. – Captain Man May 26 '15 at 19:05
  • @CaptainMan That's exactly why he said "custom" – Aify May 26 '15 at 19:17
  • @Aify That doesn't mean anything in this context, the question needs to specify something like calling [`Collections.sort(yourList, yourComparator)`](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29) to be complete. It's the difference between trying to solve the problem of getting to the moon by building a spaceship that can go to the moon and building a spaceship that can go to the moon ***and flying it to the moon***. The first isn't complete, despite the fact that the tools to solve it are present. – Captain Man May 26 '15 at 19:52
  • @CaptainMan in this context, it's more than enough. By saying custom he implies that OP must override and use the comparator. We've just given him a map. In no way whatsoever do we have to give him explicit instructions as to how to read the map. If he can't read the docs for himself I doubt anyone can help him very much. – Aify May 26 '15 at 21:00
  • @CaptainMan FWIW I wouldn't downvote an incomplete answer, since they can be quite useful to readers (OP or future visitors) who need a good hint moreso than a complete solution. If this were a stronger question, you might write a more thorough answer that would presumably get more upvotes or be accepted over an answer like mine (although mine might be accepted if it's fast and exactly what was needed). – djechlin May 26 '15 at 21:33
  • @djechlin This is a good point, but I'm not saying you should implement the Comparator in your answer, just also mention what gets the sorting done or how to use it. I do agree this needs a "hint" more than an "answer" though, this is shouldn't even have been asked, it shows no research effort at all, googling "sort an array in java based on custom condition" gets [this](http://stackoverflow.com/a/1814112/1858327) true gem of an answer. – Captain Man May 27 '15 at 14:30