假设我有一个看起来像这样的类(get / set omited):
class InfoClass{
String name;
String place;
double distance;
}
我在我的主要活动中创建了一个类的数组,如下所示:
InfoClass[3] myInfoClass;
myInfoClass[0].name = "venue one";
myInfoClass[0].place = "place one";
myInfoClass[0].distance = 11.23234;
myInfoClass[1].name = "venue two";
myInfoClass[1].place = "place two";
myInfoClass[1].distance = 9.2345643;
myInfoClass[2].name = "venue three";
myInfoClass[2].place = "place three";
myInfoClass[2].distance = 5.23432;
如何对数组(myInfoClass [])进行排序,使其由距离成员排序? 即在上面的例子中,数组会被反转,因为元素[2]的距离最小,而元素[0]的距离最大?
我可以添加一些功能来我的课程或其他方式吗?
答案 0 :(得分:5)
这应该有用..
public static void main(String[] args){
InfoClass[] dd = new InfoClass[3];
Arrays.sort(dd, new Comparator<InfoClass>(){
@Override
public int compare(InfoClass arg0, InfoClass arg1) {
// TODO Auto-generated method stub
if(arg0.distance == arg1.distance){
return 0;
}else if(arg0.distance < arg1.distance){
return -1;
}else{
return 1;
}
}
});
}
答案 1 :(得分:4)
使用java.util.Arrays.sort()
并指定您自己的Comparator
:
InfoClass[] myInfoClass = new InfoClass[3];
myInfoClass[0] = new InfoClass();
myInfoClass[1] = new InfoClass();
myInfoClass[2] = new InfoClass();
myInfoClass[0].name = "venue one";
myInfoClass[0].place = "place one";
myInfoClass[0].distance = 11.23234;
myInfoClass[1].name = "venue two";
myInfoClass[1].place = "place two";
myInfoClass[1].distance = 9.2345643;
myInfoClass[2].name = "venue three";
myInfoClass[2].place = "place three";
myInfoClass[2].distance = 5.23432;
Arrays.sort(myInfoClass,
new Comparator<InfoClass>()
{
public int compare(InfoClass o1, InfoClass o2)
{
if (o1.distance == o2.distance)
{
return 0;
}
else if (o1.distance < o2.distance)
{
return -1;
}
return 1;
}
});
答案 2 :(得分:4)
您可以将Arrays.Sort与自定义比较器一起使用,如下所示:
Arrays.Sort(myInfoClass, new Comparator<InfoClass>() {
@Override
public int compare(InfoClass o1, InfoClass o2){
if (o1==null && o2==null) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.distance.compareTo(o2.distance);
}
});
编辑: null检查胜利。
答案 3 :(得分:4)
修改您的类并实现Comparable
界面如果您不想使用Comparator
,那么默认情况下,您希望为对象的array/collection
提供排序,然后选择Comparable
class InfoClass implements Comparable<InfoClass> {
String name;
String place;
double distance;
@Override
public int compareTo(InfoClass o) {
return new Double(this.distance).compareTo(new Double(o.distance));
}
然后你可以对它们进行排序
Arrays.sort(myInfoClass)
答案 4 :(得分:2)
Arrays.sort(myInfoClass, new Comparator<InfoClass>() {
@Override
public int compare(InfoClass o1, InfoClass o2) {
return Double.valueOf(o1.distance).compareTo(o2.distance);
}
});
答案 5 :(得分:1)
将数组转换为ArrayList,然后使用Collection.sort
方法对ArrayList进行排序。
答案 6 :(得分:1)
Arrays.sort(aStudents,Collections.reverseOrder());
`public static <T> Comparator<T> reverseOrder() {
return (Comparator<T>) REVERSE_ORDER;
}
public int compare(Comparable c1,Comparable c2){ return c2.compareTo(c1); }`
答案 7 :(得分:0)
希望,此示例将帮助您排序类对象基于各个字段
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main ( String [] args) throws IOException
{
Employee empList[] = new Employee [4];
empList[0] = new Employee (8,"Kiran","Developer",15000);
empList[1] = new Employee (18,"Krishna","DBA",35000);
empList[2] = new Employee (3,"Pradeep","Tester",25000);
empList[3] = new Employee (7,"Mithun","Admin",20000);
Arrays.sort(empList);
System.out.println("\nAfter Sorting Base On ID");
for (Employee emp : empList)
System.out.println(emp);
Arrays.sort( empList , new EmployeeByDept() );
System.out.println("\nAfter Sorting Base On Department");
for (Employee emp : empList)
System.out.println(emp);
Arrays.sort( empList , new EmployeeByName() );
System.out.println("\nAfter Sorting Base On Name");
for (Employee emp : empList)
System.out.println(emp);
Arrays.sort( empList , new EmployeeBySalary() );
System.out.println("\nAfter Sorting Base On Salary");
for (Employee emp : empList)
System.out.println(emp);
}
}
class EmployeeByDept implements Comparator<Employee>
{
//compare() is mathod of Comparable Interface
// Use When we want to sort on other field like Department
@Override
public int compare(Employee emp1, Employee emp2)
{
return emp1.getDept().compareTo(emp2.getDept());
}
}
class EmployeeByName implements Comparator<Employee>
{
//compare() is mathod of Comparable Interface
// Use When we want to sort on other field like Department
@Override
public int compare(Employee emp1, Employee emp2)
{
return emp1.getName().compareTo(emp2.getName() );
}
}
class EmployeeBySalary implements Comparator<Employee>
{
//compare() is mathod of Comparable Interface
// Use When we want to sort on other field like Department
@Override
public int compare(Employee emp1, Employee emp2)
{
return ( emp1.getSalary() - emp2.getSalary());
}
}
class Employee implements Comparable<Employee>
{
int $id ;
String $name ;
String $dept ;
int $salary ;
public Employee(int id , String name , String dept , int salary)
{
this.$id = id;
this.$name = name ;
this.$dept = dept ;
this.$salary = salary ;
}
public int getID () { return this.$id ; }
public String getName () { return this.$name ; }
public String getDept () { return this.$dept ; }
public int getSalary () { return this.$salary ; }
public String toString()
{
return "[ "
+ "ID :: " + $id
+ " Name :: " + $name
+ " Department :: " + $dept
+ " Salary :: " + $salary
+ " ]";
}
//compareTo() is mathod of Comparable Interface
//Use when you want natural sorting base on ID , Salary
@Override
public int compareTo(Employee emp)
{
return (this.$id - emp.$id);
}
}