根据整数值排序对象

时间:2013-07-16 11:13:45

标签: java sorting

有一个包含私有字段Num的类条目和包含条目列表(条目)的类MyList。我需要根据Num的值对条目列表进行排序。如何以最好的方式做到这一点?我应该使用Collections.sort()吗?

public class MyList()
{
  private List<Entry> entries;

  public MyList()
  {
    ...
  }

}

public class Entry()
{
  private int Num;
  private String Val;

  public MyClass()
  {
    this.Num = 0;
    this.Val = "";
  } 

  public void setNum(int Num)
  {
    this.Num = Num;
  }

  public int getNum()
  {
   return this.Num();
  }

}

4 个答案:

答案 0 :(得分:1)

您需要编写一个实现Comparator接口的类并覆盖compare方法。然后,您可以使用集合和比较器类作为输入调用Collections.sort方法

这是一个简单的教程,可以帮助您提供样本:

http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

答案 1 :(得分:1)

public class CustomComparator implements Comparator<YourObject> {
    @Override
    public int compare(YourObject o1, YourObject o2) {
        int cmp = Integer.compare(o1.getNum(), o2.getNum());
        return comp;
    }
}

您也可以使用比较。

  

int cmp = Integer.compare(a,b); //在Java 7中

     

int cmp = Double.compare(a,b); //在Java 7之前

答案 2 :(得分:1)

使用Comparable。让Entry类通过提供compareTo()的实现来实现此接口,如果此0对象等于另一个,则应返回Entry,如果更大或为负,则返回正值比较小的一个。

public class Entry implements Comparable<Entry> // Use Generics
{
  private int Num;
  private String Val;

  public MyClass()
  {
    this.Num = 0;
    this.Val = "";
  }

  // getters/seters()

  public int compareTo(Entry otherEntry)
  {
   int num2 = otherEntry.getNum();
   return num == num2 ? 0 :
          (num > num2 ? 1 : -1);
  }

}

然后在Collections.sort()个对象的集合上使用Entry

List<Entry> lisOfEntries = new ArrayList<Entry>();
// populate the list; then sort using
Collections.sort(listOfEntries);

要使用不同的条件支持多个排序顺序,请改用Comparator#compare()

答案 3 :(得分:1)

您可以按如下方式实现Comparable接口(请注意,根据Jon Skeet的评论,我修复了一些错误并更改了几个var名称以匹配约定):

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Entry implements Comparable<Entry> {
private int num;
private String val;

public Entry() {
    this.num = 0;
    this.val = "";
} 

public void setNum(int num) {
    this.num = num;
}

public int getNum() {
    return this.num;
}

@Override
public int compareTo(Entry entry) {
    if (this.num < entry.num) return -1;
    if (this.num > entry.num) return 1;
    return 0;
}

public static void main(String[] args) {
    System.out.println("TEST 1");
    test1();

    System.out.println("\nTEST 2");
    test2();
}

public static void test1() {
    Entry e1 = new Entry();
    e1.setNum(5);

    Entry e2 = new Entry();
    e2.setNum(4);
    System.out.println("e1 = " + e1.getNum() + ", e2 = " + e2.getNum() + ", e1.compareTo(e2) = " + e1.compareTo(e2));

    Entry e3 = new Entry();
    e3.setNum(5);
    System.out.println("e1 = " + e1.getNum() + ", e3 = " + e3.getNum() + ", e1.compareTo(e3): " + e1.compareTo(e3));

    Entry e4 = new Entry();
    e4.setNum(6);
    System.out.println("e1 = " + e1.getNum() + ", e4 = " + e4.getNum() + ", e1.compareTo(e4): " + e1.compareTo(e4));
}

public static void test2() {
    List<Entry> list = new ArrayList<Entry>();
    int[] nums = { 5, 3, 9, 25, 1, -8 };
    for (int i : nums) {
        Entry e = new Entry();
        e.setNum(i);
        list.add(e);
    }
    Collections.sort(list);

    System.out.print("Orig list: ");
    for (int i : nums) {
        System.out.print(i + ", ");
    }

    System.out.println();

    System.out.print("Sorted list: ");
    for (Entry e : list) {
        System.out.print(e.getNum() + ", ");
    }

}
}