2

I have a Array list of object for class "Emp"[String eno, skill; ArrayList<Jobs> Empjobs;].

EmpJobs in itself is an arraylist for class "Jobs"[String jobId, skill;int priority, TTC;].

Now in code I created few object of Emps, say Emp1, Emp2 and they have 2 jobs assigned to each as objects of Jobs class.

job11,job12 assigned to Emp1

job21, job22 assigned to Emp2

I need to sort Arraylist of Emp based on total of their jobs TTC(Time to Completeion). I tried using comparor method but no succeess. Any help will be much appreciated.below are snippets of code.

Also to add,

Emp array values are like : 'EmpId#Skill'

Job Array values are like : 'skill#priority#TTC#JobId'

I am not able to work out Collections.sort(emp2,new MyComparator());

class EmpJobsAssign{
    class Emp
    {
    String eno, skill;
    ArrayList<Jobs> Empjobs;
    public Emp(){
        }
    @Override
    public String toString() {
        return ("eno:"+this.eno+
                " skill: "+ this.skill+
                " Job: "+  this.Empjobs);
        }
    }

public class MyComparator implements Comparator<Emp> {
@Override
public int compare(Emp e1, Emp e2) {
    if (e1.Empjobs.TTC > e2.Empjobs.TTC)
     {
         return 1;
     }
    else if (e1.Empjobs.TTC > e2.Empjobs.TTC)
    {
        return -1;
    }
    return 0;    
} 
}

class Jobs
    {
    String jobId, skill;
    int priority, TTC;
    public Jobs(){
     }
     @Override
    public String toString() {
        return ("jobId:"+this.jobId+
                " skill: "+ this.skill +
                " priority: "+ this.priority +
                " TTC: "+ this.TTC);
        }
     }

public static void main(String []args){

        String[] arrayEmp = {"w4#j","w1#c","w2#c","W3#j"}; 
        String[] arrayJob = {"c#3#25#obj1","j#2#20#obj2","j#1#45#obj3","c#4#45#obj4","c#1#15#obj5"}; 
        EmpJobsAssign HW =new EmpJobsAssign();
        EmpJobsAssign.Emp emp=HW.new Emp();
        EmpJobsAssign.Jobs job=HW.new Jobs(); 
        Emp[] emps= HW.SortEmp(arrayEmp,0);
        Jobs[] empjobs=HW.SortJob(arrayJob,1,2);
        int jobindex=0;
        while(jobindex < empjobs.length)
        {
         job=empjobs[jobindex];
            ArrayList<Emp> emp2=new ArrayList<Emp>(); 
             String AssignedFlag="";
            for(int empindex=0;empindex<emps.length;empindex++)
            {
                AssignedFlag="F";
                emp=emps[empindex];
               if(emp.Empjobs == null) {emp.Empjobs=new ArrayList<Jobs>();}
                if(emp.skill.equals(job.skill) && emp.Empjobs.isEmpty()){
                    emp.Empjobs.add(job);
                    System.out.println("empjobs : " +emp);  
                    AssignedFlag="T";
                    System.out.println("Emp2  "+ emp2.size());
                    break;
                }
            }
            System.out.println(" AssignedFlag "+AssignedFlag);
            if(AssignedFlag.equals("F")) {
                Collections.sort(emp2,new MyComparator());
                 System.out.println("Inside If  "+ emp2.get(0));
                Iterator itr=emp2.iterator(); 
                while(itr.hasNext()){
                    System.out.println("test "+ itr.next());
                }
            }
            jobindex++;
        }
        System.out.println("check");
        System.out.println(Arrays.toString(emps));
        System.out.println(Arrays.toString(empjobs)); 
 }

private Emp[] SortEmp(String[] a, int index1)
    {
        Emp[] SortEmp=new Emp[a.length];
        for(int i=0;i<a.length;i++){
            for(int j=i+1;j<a.length;j++){
                if(Integer.parseInt(a[i].substring(1,2))>Integer.parseInt(a[j].substring(1,2))){
                    String temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
                }
                Emp tempEmp = new Emp();
                tempEmp.eno=a[i].split("#")[0];
                tempEmp.skill=a[i].split("#")[1];
            SortEmp[i]=tempEmp;
        }
        return SortEmp;
    }

    private Jobs[] SortJob(String[] a, int index1, int index2)
    {
        Jobs[] SortJobs=new Jobs[a.length];
        for(int i=0;i<a.length;i++){
            for(int j=i+1;j<a.length;j++){
                if(Integer.parseInt(a[i].split("#")[index1])>Integer.parseInt(a[j].split("#")[index1])){
                    String temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                }
                 if(Integer.parseInt(a[i].split("#")[index1])==Integer.parseInt(a[j].split("#")[index1])){
                    if(Integer.parseInt(a[i].split("#")[index2]) > Integer.parseInt(a[j].split("#")[index2])){
                    String temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
                    }
                }
                }
                Jobs tempJobs=new Jobs();
                tempJobs.jobId=a[i].split("#")[3];
                tempJobs.skill=a[i].split("#")[0];
                tempJobs.priority=Integer.parseInt(a[i].split("#")[1]);
                tempJobs.TTC=Integer.parseInt(a[i].split("#")[2]);
            SortJobs[i]=tempJobs;
        }
        return SortJobs;
    }
  }
4
  • 2
    Unrelated: read about java naming conventions. Your names are sorry, bizarre. Variables go camelCase (always). Also: dont abbreviate, especially on class names (like Emp - plain horrible). Commented Jul 7, 2017 at 10:56
  • Thanks for your feedback Ghostcat. Well this is my first professional java code. I will work on your feedbacks. Commented Jul 7, 2017 at 11:08
  • 2
    Don'T get me wrong, but this code is about a zillion miles away from "professional". I hope you simply mean that this is like your first "real" attempt to solve an assignment with java. Commented Jul 7, 2017 at 11:17
  • 2
    Unrelated two: you want us to spend our time to help you solve your problem. So you please: A) put up code that compiles (when it doesnt compile, give exact error messages) B) put up code that is correctly indented and formatted. Thing is: your code is just 10 times harder to read then it ought to be. Commented Jul 7, 2017 at 14:23

2 Answers 2

3

Don't put down your own comparison if you don't have to.

Your compare() boils down to:

return Integer.compare(e1.Empjobs.TTC, e2.Empjobs.TTC);

Completely eliminating the chance of introducing such subtle typos as your original code is showing (where you simply did a < b, a b).

Sign up to request clarification or add additional context in comments.

2 Comments

Below error is seen on compilaton: HelloWorld.java:23: error: cannot find symbol if (e1.Empjobs.TTC > e2.Empjobs.TTC) symbol: variable TTC
I just copied your code here. I am telling you: you don't need to compare yourself, when you have two int values, you can use Integer.compare(a, b) to compare them. When you now find that your own code e1.empjobs.TTC is invalid ... well. That is your code.
2

The comparator is incorrectly implemented, both if statements have the same predicate.

@Override
public int compare(Emp e1, Emp e2) {
    if (e1.Empjobs.TTC > e2.Empjobs.TTC)      // predicate 1
     {
         return 1;
     }
    else if (e1.Empjobs.TTC > e2.Empjobs.TTC) // predicate 2, same as predicate 1
    {
        return -1;
    }
    return 0;    
} 

Change the second predicate to e1.Empjobs.TTC < e2.Empjobs.TTC.

1 Comment

Below error is seen on compilaton: HelloWorld.java:23: error: cannot find symbol if (e1.Empjobs.TTC > e2.Empjobs.TTC) symbol: variable TTC

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.