Collection.sort的问题

时间:2013-11-13 18:54:34

标签: java sorting

使用Collection.sort时,我一直收到绑定不匹配,我不知道如何开始修复此问题。调用它的方法是:

    static LinkedList <Car> CarList = new LinkedList<Car>(); //Loaded LinkedList

public void DisplayAlphabetical() {
     Collections.sort(CarList);
} //End of Method DisplayAlphabetical

LinkedList使用来自另一个类的Car参数创建要排序的汽车列表:

public class Car {
private String Model = "";
private String Colour = "";
private int Year = 0;
private int VIN = 0;
private double Price = 0;
static int TotalCars;

//Car Constructor
public Car (String Model, String Colour, int newYear, int newVIN, double newPrice){
    this.Model = Model;
    this.Colour = Colour;
    this.Year = newYear;
    this.VIN = newVIN;
    this.Price = newPrice;
    TotalCars++;
} //End of Constructor Car

//Get the car's model
public String getModel() {
    return Model;
} //End of Method getModel

//Get the car's colour
public String getColour() {
    return Colour;
} //End of Method getColour

//Get the year of the car
public int getYear() {
    return Year;
} //End of Method getYear

//Get the VIN of the car
public int getVIN() {//static Car C = new Car(Model, Colour, Year, VIN, Price);
    return VIN;
} //End of Method getVIN

//Get the price of the car
public double getPrice() {
    return Price;
} //End of Method getPrice

我知道Collection.sort需要具有可比性,但我无法弄清楚如何正确实现它。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

我通常更喜欢将 Comparator Collections.sort(List,Comparator)一起使用,而不是实施可比较:

public class ComparatorTest {

    public static class Car {
        private String model;
        public Car(String model) {
            this.model = model;
        }
        public String getModel() {
            return model;
        }
    }

    public static void main(String[] args) {
        LinkedList<Car> list = new LinkedList<Car>();
        list.add(new Car("Golf"));
        list.add(new Car("Fiesta"));            
        Collections.sort(list, new Comparator<Car>() {
            public int compare(Car o1, Car o2) {
                String car1Model = o1.getModel();
                String car2Model = o2.getModel();
                // TODO! return a value!
                // Read http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html#compare(T,%20T) for more information about what to return
            }
        });
    }
}

答案 1 :(得分:1)

public class Car implements Comparable {
    ...
    public int compareTo(Car c){
        // Implement how you think a car is compared to another
        //Returns a negative integer, zero, or a positive integer as this object is less    
        //than, equal to, or greater than the specified object.
   }
}

答案 2 :(得分:0)

使用Collectio.sort()对单个和多个条件进行排序

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;    
import java.util.Comparator;    
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.DateFormat;

public class SortingWithCollection {

public static void main(String[] args) {

    List<Student> listStudent = new ArrayList<Student>();

    listStudent.add(new Student("Rahul", "01", 45, "2020-11-15T11:01:10.000Z"));
    listStudent.add(new Student("Dharma", "08", 30, "2020-11-15T11:22:50.000Z"));
    listStudent.add(new Student("Lalit", "10", 45, "2020-11-15T11:05:10.000Z"));
    listStudent.add(new Student("Harshit", "02", 25,"2020-11-15T11:22:10.000Z"));
    listStudent.add(new Student("Mohit", "05", 45, "2020-11-15T11:12:30.000Z"));
    listStudent.add(new Student("Ram", "09", 30,"2020-11-15T11:22:10.000Z"));
    listStudent.add(new Student("Lalu", "10", 25, "2020-11-15T11:28:10.000Z"));
    listStudent.add(new Student("Sohan", "12", 30, "2020-11-15T11:22:10.000Z"));
    listStudent.add(new Student("Neha", "04", 22, "2020-11-15T11:22:10.000Z"));
    listStudent.add(new Student("Aaradhya", "01", 28,"2020-11-15T11:22:14.000Z"));
    listStudent.add(new Student("Aakriti", "06", 35, "2020-11-15T11:22:10.000Z"));
    listStudent.add(new Student("Sonali", "05", 35, "2020-11-15T11:35:01.000Z"));
    listStudent.add(new Student("Billo", "08", 35, "2020-11-15T11:22:10.000Z"));
    listStudent.add(new Student("Sohan", "05", 35, "2020-11-15T11:22:10.000Z"));
    listStudent.add(new Student("Rohan", "02", 30, "2020-11-15T11:58:12.000Z"));

    System.out.println("Before sorting List Is:");

    for (Student student : listStudent) {
        System.out.println(student);
    }

    Collections.sort(listStudent,new SortByAgeAndDate());

    // Collections.sort(listStudent,new SortByClass());

    System.out.println("After sorting List is:");

    for (Student student : listStudent) {
        System.out.println(student);
    }

}

public static class SortByClass implements Comparator<Student> {
    @Override
    public int compare(Student student1, Student student2) {
        return student2.getClassName().compareTo(student1.getClassName());
    }
}

public static class SortByAgeAndDate implements Comparator<Student>{
    @Override
   public int compare(Student student1, Student student2) {
       if(student1.getAge()==student2.getAge()){
        return 
student2.getDateCreatedAt().compareTo(student1.getDateCreatedAt());
       }else{
           return Integer.compare(student2.getAge(),student1.getAge());
        }
   }
}

public static class Student {
 String name;
 String className;
 int age;
 String createdAt;
 Date dateCreatedAt;


public Student(String name, String className, int age, String createdAt) {
    this.name = name;
    this.className = className;
    this.age = age;
    this.createdAt=createdAt;
}

public int getAge(){
    return age;
}

public String getName(){
    return name;
}

public String getClassName(){
    return className;
}

public String getCreatedAt(){
    return createdAt;
}

public Date getDateCreatedAt(){
    try{
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        dateCreatedAt = dateFormat.parse(createdAt);
    }catch(Exception e){
        System.out.println(""+e.getMessage());
        return null;
    }
    return dateCreatedAt;
    }
public String toString() {
    return String.format("%s\t%s\t%d\t%s", name, className, age,createdAt);
}
}

}