The task is to sort the objects. You must create a list of twenty objects. Each object is a room with a randomly given width and length.

The RoomComporator class implements the Comporator interface. I want to sort the rooms according to their area using the following command: Collection.sort (list, new RoomComparator ())

Sort the list to display on the console. The question is that I don’t understand how Collection.sort works when there is an object of another class in the argument. My code returns zeros.

Here is the code for the RoomComporator class:

import java.util.Comparator; public class RoomComporator implements Comparator<Room> { public RoomComporator(){} @Override public int compare(Room r1, Room r2 ) { return r1.square- r2.square;} } 

And the code for the main class Room:

 import java.util.List; import java.util.Collections; import java.util.LinkedList; public class Room { int length; int width; int square; public int getWidth(){ return this.width; } public void setWidth(int width){ this.width = width; } public int getLength(){ return this.length; } public int setLength(int length){ return this.length = length; } public Room(int l, int w) { this.length = l; setWidth(w); } void square(){ int l, w; l = length; w = width; int s = w*l; System.out.println(s); } Room (int square){ this.square=square; } void printRoom(){ System.out.println(length + " " + width); } public static void main(String[] args){ List<Room> rooms = new LinkedList <Room>(); int numRooms = 20; int maxLength = 9; int maxWidth = 9; for( int i = 0; i < numRooms; i++) { int length = 1 + (int)(Math.random()*maxLength); int width = 1 + (int)(Math.random()*maxWidth); Room r = new Room(length, width); System.out.println("Room " + (i + 1)); r.printRoom(); r.square(); rooms.add(r); } for( int i = 0; i < numRooms; i++){ Collections.sort(rooms, new RoomComporator()); System.out.println(rooms.get(i).square);} } } 
  • one
    And why do you think that square will be different from zero if you don’t change it (you don’t call the corresponding constructor)? - Vartlok

2 answers 2

Instead of implementing a comparator, implement the Comparable interface for Room

  public class Room implements Comparable { public int compareTo(Room room){ return this.square - room.square; 

http://metanit.com/java/tutorial/5.6.php

    Your Room class must implement the Comparable interface Comparable

     List<MyObject> list = new ArrayList<MyObject>(); Comparator<MyObject> comparator = new Comparator<MyObject>() { @Override public int compare(MyObject left, MyObject right) { return left.getId() - right.getId(); } }; Collections.sort(list, comparator); System.out.println(list); 

    Java 8 using lambda

     List<MyObject> list = new ArrayList<MyObject>(); Collections.sort(list, (left, right) -> left.getId() - right.getId()); System.out.println(list); 

    Detailed example ...

     import java.util.*; class Dog implements Comparator<Dog>, Comparable<Dog> { private String name; private int age; Dog() { } Dog(String n, int a) { name = n; age = a; } public String getDogName() { return name; } public int getDogAge() { return age; } // Overriding the compareTo method public int compareTo(Dog d) { return (this.name).compareTo(d.name); } // Overriding the compare method to sort the age public int compare(Dog d, Dog d1) { return d.age - d1.age; } } public class Example { public static void main(String args[]) { // Takes a list o Dog objects List<Dog> list = new ArrayList<Dog>(); list.add(new Dog("Shaggy", 3)); list.add(new Dog("Lacy", 2)); list.add(new Dog("Roger", 10)); list.add(new Dog("Tommy", 4)); list.add(new Dog("Tammy", 1)); Collections.sort(list); // Sorts the array list for(Dog a: list) // printing the sorted list of names System.out.print(a.getDogName() + ", "); // Sorts the array list using comparator Collections.sort(list, new Dog()); System.out.println(" "); for(Dog a: list) // printing the sorted list of ages System.out.print(a.getDogName() +" : "+ a.getDogAge() + ", "); } }