So im trying to sort a list of Car objects based off of whether the price of the car is even or not. SortEven sorts so that odd float to top however i need evens to float to top. but to me, the logic of SortEven should be swapping so that evens float to top Can someone explain how SortEven is working?
package Consumables;
import java.util.Comparator;
public class Cars implements Comparable<Cars>{
private String registrationNumber;
private int price;
private int seats;
private int mpg;
public static final Comparator<Cars> BY_MPG = new ByMPG();
public static final Comparator<Cars> BY_SEATS = new BySeats();
public static final Comparator<Cars> SORT_EVEN = new SortEven();
public Cars(String regNumber) {
this.setRegistrationNumber(regNumber);
}
private static class ByMPG implements Comparator<Cars> {
public int compare(Cars t, Cars c) {
return t.getMpg() - c.getMpg();
}
}
private static class BySeats implements Comparator<Cars> {
public int compare(Cars t, Cars c) {
return t.getSeats() - c.getSeats();
}
}
What im trying to do/ logic: t.getPrice() % 2 evaluate to 0 if even. so i check the first object. if true t keeps location, if false, c.getPrice() is check to see if it is odd because if it is even that will be checked on second pass. finally return 0.
private static class SortEven implements Comparator<Cars> {
public int compare(Cars t, Cars c) {
if ((t.getPrice() % 2) == 0)
return 1;
else if ((c.getPrice() % 2) != 0)
return -1;
return 0;
}
}
public String getRegistrationNumber() {
return registrationNumber;
}
public void setRegistrationNumber(String registrationNumber) {
this.registrationNumber = registrationNumber;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSeats() {
return seats;
}
public void setSeats(int seats) {
this.seats = seats;
}
public int getMpg() {
return mpg;
}
public void setMpg(int mpg) {
this.mpg = mpg;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj != null && obj instanceof Cars) {
String regNumber = ((Cars)obj).getRegistrationNumber();
if (regNumber != null && regNumber.equals(this.registrationNumber))
return true;
}
return false;
}
@Override
public int hashCode() {
return this.registrationNumber.hashCode();
}
@Override
public int compareTo(Cars o) {
// TODO Auto-generated method stub
if (o != null && o instanceof Cars) {
if (this.getPrice() > o.getPrice())
return 1;
else if (this.getPrice() < o.getPrice())
return -1;
}
return 0;
}
@Override
public String toString() {
return "Registration Number: " + this.getRegistrationNumber()
+ "\n Price: " + this.getPrice()
+ "\n # of Seats : " + this.getSeats()
+ "\n MPG : " + this.getMpg();
}
}
Generating values randomly
import Consumables.Cars;
/**
* @author briensmarandache
*
*/
public class ComparatorSketchpad {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Cars> listOfCars = new ArrayList<>();
for (int i=0; i < 100; i++) {
Cars car = new Cars("H0-" + i);
car.setMpg((int)(Math.random() * ((40 - 20) + 1)) + 20);
car.setSeats((int)(Math.random() * ((8 - 2) + 1)) + 2);
car.setPrice((int)(Math.random() * ((80_000 - 5_000) + 1)) + 5_000);
listOfCars.add(car);
}
Collections.sort(listOfCars, Cars.SORT_EVEN);
System.out.println("============ Print sorted by even price list ==========");
Iterator<Cars> carIteratorByEven = listOfCars.iterator();
while (carIteratorByEven.hasNext()) {
System.out.println(carIteratorByEven.next());
}
}
}
This solves the issue, but im having trouble understanding why it works. it seems is it is operating backwards
private static class SortEven implements Comparator<Cars> {
public int compare(Cars t, Cars c) {
if ((t.getPrice() % 2) == 0 && (c.getPrice() % 2) == 0)
return 0;
else if ((t.getPrice() % 2) == 0 || (c.getPrice() % 2) != 0)
return -1; // walk through debugger
return 1;
}
Making suggested changes (ie. using && and complete case comparisons) i have.....
private static class SortEven implements Comparator<Cars> {
public int compare(Cars t, Cars c) {
if (((t.getPrice() % 2) == 0 && (c.getPrice() % 2) == 0) || (t.getPrice() % 2) != 0 && (c.getPrice() % 2) != 0 )
return 0; // no swap
else if ((t.getPrice() % 2) == 0 && (c.getPrice() % 2) != 0)
return 1; // no swap // walk through debugger
return -1; // swap
}
However this sort so that odd float to the top, opposite to what needs to happen