Klausos Klausos
Klausos Klausos

Reputation: 16050

Order objects according to integer values

There is a class Entry with the private field Num and the class MyList that contains List of entries (Entry). I need to sort List of entries according to the values of Num. How to do this in the best way? Should I use 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();
  }

}

Upvotes: 0

Views: 106

Answers (4)

neizan
neizan

Reputation: 2321

You could implement the Comparable interface as follows (note I fixed a couple of mistakes and changed a couple var names to match conventions, per Jon Skeet's comment):

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() + ", ");
    }

}
}

Upvotes: 1

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

Use Comparable. Make your Entry class implement this interface by providing the implementation for compareTo() which should return a 0 if this Entry object equals the other, a positive value if bigger or a negative one if small in comparison.

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

}

Then use Collections.sort() on a collection of Entry objects.

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

To support multiple sort orders using different criteria, use Comparator#compare() instead.

Upvotes: 1

Waqas Memon
Waqas Memon

Reputation: 1249

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

You can use compare as well.

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

int cmp = Double.compare(a, b); // before Java 7

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

You need to write a class implementing Comparator interface and overriding the compare method. Then you can call the Collections.sort method with your collection and comparator class as the inputs

Here is a simple tutorial to help you with samples:

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

Upvotes: 1

Related Questions