Results 1 to 3 of 3
  1. #1
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default object list sorting

    Hey!


    If i want to sort a object list by a random variable do I really have to make implementation class for every variable?

    example:
    list of car objects
    Java Code:
    class car {
        int id;
        String brand;
        int year;
    //Setters & getters
    
    }
    class carID implements Comparable{
    public int compare(Object car, Object anotherCar) {
        int id = car.getId();
        int id2 =  anotherCar.getId();
       
          return id.compareTo(id2);
        
      }
    }
    class carBrand implements Comparable{
    public int compare(Object car, Object anotherCar) {
        String brand = car.getBrand();
        String brand2 =  anotherCar.getBrand();
       
          return brand.compareTo(brand2);
        
      }
    }
    
    .....
    
    Or is there another way?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    Class names should start with upper-case letters. But you could also do something like this:

    Java Code:
    class carID implements Comparable{
    public int compare(Object car, Object anotherCar) {
       if(condition){
          int id = car.getId();
          int id2 =  anotherCar.getId();
          return id.compareTo(id2);
       }
       else{
           String brand = car.getBrand();
           String brand2 =  anotherCar.getBrand();
           return brand.compareTo(brand2);
        }
    }
    Then just set the condition of the comparing class whenever you want to use it. I don't really see the "problem" you're trying to work around, though.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    You also might want to use an anonymous inner class for each sort:

    Java Code:
    Collections.sort(yourList, new Comparator<Car>(){
       public int compareTo(Car car1, Car car2){
          //...
       }
    });
    Again, depends on your requirements, which you haven't really explained.

Similar Threads

  1. Need help with sorting a linked list
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-04-2010, 02:15 AM
  2. list sorting problem.
    By bit_bit in forum Advanced Java
    Replies: 2
    Last Post: 02-26-2010, 05:17 AM
  3. List Sorting method.
    By bit_bit in forum New To Java
    Replies: 1
    Last Post: 02-24-2010, 12:44 PM
  4. Sorting a linked list
    By Hayzam in forum New To Java
    Replies: 4
    Last Post: 01-18-2009, 01:07 AM
  5. Converting array to list and sorting it
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •