简体   繁体   English

Collection.sort 的问题

[英]Problems with Collection.sort

I keep getting the Bound Mismatch when using Collection.sort, I have no idea how to begin fixing this problem.使用 Collection.sort 时,我一直遇到 Bound Mismatch,我不知道如何开始解决这个问题。 The method that calls it is this:调用它的方法是这样的:

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

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

The LinkedList creates a car list to be sorted, using the parameters of Car from another class: 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

I understand that Collection.sort needs a comparable in it, but I haven't been able to figure out how to properly implement it.我知道 Collection.sort 需要一个可比较的,但我一直无法弄清楚如何正确实现它。 Any help would be appreciated.任何帮助,将不胜感激。

I usually prefer to use Comparator with Collections.sort(List,Comparator) instead of implementing Comparable:我通常更喜欢将ComparatorCollections.sort(List,Comparator)一起使用而不是实现 Comparable:

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
            }
        });
    }
}
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.
   }
}

Sort With Single and Multiple Condition with Collectio.sort()使用 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);
}
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM