我在pojo类中的单值中有很多类型的例子。
但我希望使用多个值进行排序,如下例所示
排序前
Name City Roll
--------------------------------------
sekhar ---------bbsr------------23
himanshu -------agra------------23
nitin ----------delhi------------23
nitin ----------delhi------------22
nitin ----------bbsr------------23
arun -----------patna------------23
arun -----------kendrapara-------23
排序后
Name City Roll
--------------------------------------
arun -------kendrapara------------23
arun -------patna------------23
himanshu ----agra------------23
nitin -------bbsr------------23
nitin -------delhi------------22
nitin -------delhi------------23
sekhar -------bbsr------------23
我的代码在这里
package example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo demo = new Demo();
List<Student> list = new ArrayList<Student>(); // your Car list
Student st = demo.new Student();
st.setName("sekhar");
st.setCity("bbsr");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("himanshu");
st.setCity("agra");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("nitin");
st.setCity("delhi");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("nitin");
st.setCity("delhi");
st.setRoll(22);
list.add(st);
st = demo.new Student();
st.setName("nitin");
st.setCity("bbsr");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("arun");
st.setCity("patna");
st.setRoll(23);
list.add(st);
st = demo.new Student();
st.setName("arun");
st.setCity("kendrapara");
st.setRoll(23);
list.add(st);
System.out.println("before sort");
System.out.println("Name:" + "-------------------" + "City...........roll");
for (Student st1 : list) {
System.out.println(st1.getName() + " --------------" + st1.getCity() + "------------" + st1.getRoll());
}
Collections.sort(list, demo.new CarHorsePowerComparator());
System.out.println("after sort");
System.out.println("Name:" + "-------------------" + "City...............roll");
for (Student st1 : list) {
System.out.println(st1.getName() + " --------------" + st1.getCity() + "------------" + st1.getRoll());
}
}
public class Student {
String name;
String city;
int roll;
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class CarHorsePowerComparator implements Comparator<Student> {
public int compare(Student stu, Student stu1) {
int sort = (stu.getName() + stu.getCity() + stu.getRoll()).compareTo((stu1.getName() + stu1.getCity() + stu1.getRoll()));
return sort;
}
}
}
但我使用自定义比较器
class CarHorsePowerComparator implements Comparator<Student> {
public int compare(Student stu, Student stu1) {
int sort = (stu.getName() + stu.getCity() + stu.getRoll()).compareTo((stu1.getName() + stu1.getCity() + stu1.getRoll()));
return sort;
}
它对我有用,但这不是正确的方法。 如何以正确的方式编写此代码请建议我。
int sort = (stu.getName() + stu.getCity() + stu.getRoll()).compareTo((stu1.getName() + stu1.getCity() + stu1.getRoll()));
答案 0 :(得分:3)
(stu.getName() + stu.getCity() + stu.getRoll()).compareTo((stu1.getName() + stu1.getCity() + stu1.getRoll()))
表示您要对结果字符串进行名称,城市和滚动编号以及排序。
您应首先确定排序优先级,然后按顺序对字段进行排序。 看看预期的结果似乎你想首先在名字上排序然后在城市上排序,最后在滚动上排序。
class CarHorsePowerComparator implements Comparator<Student> {
public int compare(Student stu, Student stu1) {
int sort;
// Sort on Name first
sort = stu.getName().compareTo(stu1.getName);
if(sort == 0){
// If name is equal, sort on city
sort = stu.getCity().compareTo(stu1.getCity);
}
if(sort == 0){
// If name and city is equal, sort on Roll
sort = Integer.compare(stu.getRoll(),stu1.getRoll());
}
return sort;
}
}
答案 1 :(得分:0)
int sort = stu.getName().compareTo(stu1.getName());
if (sort == 0)
sort = stu.getCity().compareTo(stu1.getCity())
if (sort == 0)
...
return res;
关键是只有当前组件相等时才继续比较
答案 2 :(得分:0)
当您使用plus运算符时,您将创建许多新的String对象。 String应该用于存储一些不是出于逻辑目的的数据。
您不应将参数名称用作stu
和stu1
,因为它容易出错。您可以使用me
和other
或left
和'右'。
您不需要类声明,因为您将重复使用它。匿名是好的
private final static Comparator<Student> STUDENT_NAME_CITY_ROLL_COMPARATOR = new Comparator<Student> {
private final Collator collator = Collator.getInstance(); //Default local is taken.
public int compare(Student left, Student right) {
int result = collator.compare(left.getName(),right.getName());
if(result == 0) {
result = collator.compare(left.getCity(),right.getCity());
}
if(result == 0) {
result = Integer.compare(left.getRoll(), right.getRoll()); //JDK 7
//result = Double.compare(left.getRoll(), right.getRoll());
}
return result;
}
注意:当某些元素为null时,实现将抛出NPE。