This discussion is archived
4 Replies Latest reply: Nov 7, 2010 7:11 AM by camickr RSS

Comparator to sort the elements in Odd and Even order

800839 Newbie
Currently Being Moderated
Hi,

I have a list which contains values from 1 to 10. I want to sort the values in Odd and even order using comparator, like:-

1
3
5
7
9
2
4
6
8
10

It compareTo (obj1, obj2) method takes objects as a parameter. Hence how to sort this values in above order? Please answer.
Thanks in advance.
  • 1. Re: Comparator to sort the elements in Odd and Even order
    Kayaman Guru
    Currently Being Moderated
    Well, that's pretty simple.
    If you're comparing an even and an odd number, you sort the odd one as being smaller.
    If you're comparing 2 even or 2 odd numbers, you sort them according to the value normally.

    If you're wondering about compareTo taking objects as parameters, you'll just need to cast them to Integers (or use generics).
  • 2. Re: Comparator to sort the elements in Odd and Even order
    800839 Newbie
    Currently Being Moderated
    Thanks Kayaman. I am trying to do this using Comparator Interface using Compare(Object1,Object2).

    I have a Employee class which takes the following fields

    public class Employee {  
    private int empId;
    private String name;
    private int age; public Employee(int empId, String name, int age) {        // set values on attributes    } // getters & setters}

    I have Util, which does have:-

    import java.util.*;
    public class Util { 
    public static List<Employee> getEmployees() {       
    List<Employee> col = new ArrayList<Employee>();
    col.add(new Employee(5, "Frank", 28));
    col.add(new Employee(1, "Jorge", 19));
    col.add(new Employee(6, "Bill", 34));
    col.add(new Employee(3, "Michel", 10));
    col.add(new Employee(7, "Simpson", 8));
    col.add(new Employee(4, "Clerk",16 ));
    col.add(new Employee(8, "Lee", 40));
    col.add(new Employee(2, "Mark", 30));
    return col; }}

    I have written a CustomComparator

    public class EmpSortByEmpId implements Comparator<Employee>
    {  
    public int compare(Employee o1, Employee o2) {     
    return o1.getEmpId() - o2.getEmpId();
    }

    I have a Test class to test it:-

    import java.util.*;
    public class TestEmployeeSort {    
    public static void main(String[] args) {       
    List coll = Util.getEmployees();
    //use Comparator implementation
    Collections.sort(coll, new EmpSortByEmpId ());
    printList(coll);
    }
    private static void printList(List<Employee> list) {   
    System.out.println("EmpId\tName\tAge");
    for (Employee e: list) {      
    System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
    } }}

    This will display the EmpId in Soring order

    Like :-

    1 Jorge 19
    2 Mark 30
    3 Michel 10
    ....

    But how i can sort this in the "Odd" and "Even" order
    Like

    Empid
    1
    3
    5
    7
    9
    2
    4
    6
    8
    10.

    Please clarify. Thanks in advance.
  • 3. Re: Comparator to sort the elements in Odd and Even order
    Kayaman Guru
    Currently Being Moderated
    Well, you know (or at least should know) how the compareTo() method works.
    It returns a negative value if o1 is "less" (should be sorted before) than o2, 0 if they're equal and a positive value if o1 is "more" (should be sorted after) than o2.

    I outlined the logic in my previous reply.
  • 4. Re: Comparator to sort the elements in Odd and Even order
    camickr Expert
    Currently Being Moderated
    After 23 postings you should know how to:

    a) use the code foramtting tags. Read the "Welcome to the new home" entry at the top of every forum.

    b) change your screen name to something other than the assigned number.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points