0

i want to print the salaries in asending order the last function which is called arrange must do the job either than that the code is working i cant do it with (collections.sort();)

public static void main(String[] args) {

    ArrayList<Employee>al=new ArrayList<>();
    ArrayList<Employee>all=new ArrayList<>();

    Add(al);
    Avg(al);
    Count(al,all);
    Arrange(al);
}

static void Add(ArrayList<Employee> arr){
        Scanner s=new Scanner(System.in);
         for(int i=0;i<2;i++)
         arr.add(new Employee(s.next(),s.next(),s.nextInt()));

}

 static class Employee{
     String name;
     String department;
     Integer salary;

     Employee(String name,String department,Integer salary){
      this.name=name;
      this.department=department;
      this.salary=salary;
 }

}

 static void Arrange(ArrayList<Employee> arr){
         for(Employee a:arr)

         System.out.println(a.salary);}

 }
sultanduwaik
  • 11
  • 1
  • 4
  • Override the equals() method on the Employee class, comparing on salary, then call Collections.sort() , passing you ArrayList – pczeus Apr 28 '20 at 04:15
  • *i cant do it with (collections.sort();)* Why not? Asking because of course you can, and it’s a good solution. Are you not allowed to? Do you think it has unwanted side effects? Can’t you find out how? (and if so, what did you try?) – Ole V.V. Apr 28 '20 at 04:52
  • Please format your code nicely. It noticeably improves the chance that people will read and understand it, which in turn improves the chance of a good answer. – Ole V.V. Apr 28 '20 at 04:54
  • Does this answer your question? [Collections sort(List,Comparator super T>) method example](https://stackoverflow.com/questions/14154127/collections-sortlistt-comparator-super-t-method-example) – Dusayanta Prasad Apr 28 '20 at 06:23
  • 1
    https://stackoverflow.com/questions/14154127/collections-sortlistt-comparator-super-t-method-example See if this helps – Dusayanta Prasad Apr 28 '20 at 06:24

0 Answers0