4

假设我有一个看起来像这样的类(get/set 省略):

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] 的距离最大?

是否有一些功能可以添加到我的类中来执行此操作或其他方式?

4

8 回答 8

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;
            }
        }
    });
}
于 2012-06-15T12:49:50.750 回答
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;
                }
            });
于 2012-06-15T12:48:25.413 回答
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);
    }
});

编辑:空检查是否获胜。

于 2012-06-15T12:49:46.740 回答
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)
于 2012-06-15T12:57:28.000 回答
2
Arrays.sort(myInfoClass, new Comparator<InfoClass>() {
  @Override
  public int compare(InfoClass o1, InfoClass o2) {
    return Double.valueOf(o1.distance).compareTo(o2.distance);
  }
});
于 2012-06-15T12:52:52.433 回答
1

将数组转换为 ArrayList,然后使用Collection.sort方法对 ArrayList 进行排序。

于 2012-06-15T12:49:46.050 回答
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); }`

于 2012-06-15T13:27:47.720 回答
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);
    }


}
于 2015-03-24T13:21:18.613 回答