Results 1 to 5 of 5
  1. #1
    hongi is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default sort array >> need help

    This is mycode sofar.
    I already figure it out how to sort by name but i can't sort by price because compareTo don't accept int.
    Any suggestion ?

    Java Code:
    import java.util.*;
    public class test
    {
       public static void main (String[] args)
       {
          Property[] Properties = 
          {
             new Property(1234,"San Jose"), 
             new Property(9999,"Olive"),
             new Property(5555,"Mountain View"), 
             new Property(4444,"Palo Altos"),
             new Property(5333,"Foothill"),
          };
      
          Property.PrintArrayConsole("before:", Properties);
          Property.ArraySort(Properties);
          Property.PrintArrayConsole("After:", Properties);
    
        }
    }
    
    class Property
    {
       private static boolean FloatLargestToTop(Property[] data, int top)
       {
          boolean changed = false;
          Property temp;
          
          // notice we stop at length -2 because of expr. k+1 in loop
    
          for (int k =0; k < top; k++)   
             if (compareTo(data[k],"PRICE") > 0)
             {
                temp = data[k];
                data[k] = data[k+1];
                data[k+1] = temp;
                changed = true;
             }
           return changed;
       }
       public static int compareTo(Property prep,String PRICE)
       {
          Property tmp = (Property)prep;
          if(this.price < tmp.price)
          {
            /* instance lt received */
            return -1;
          }   
          else if(this.price > tmp.price)
          {
            /* instance gt received */        
            return 1;
          }
          /* instance == received */
          return 0;  
          
       }
     
       
        public static void ArraySort(Property[] array)
        {
           for (int k = 0; k < array.length; k++)
              if ( !FloatLargestToTop(array, array.length-1-k) )
                 return;
        
        }
       // print out array with string as a title for the message box
       public static void PrintArrayConsole(String title, Property[] data)
       {
          
          System.out.println("\n********** " + title + " **********\n");
          for (int k =0; k < data.length; k++)
             System.out.println(  data[k].price+"  " + data[k].address );
          System.out.println();
      }
    
       // returns true if a modification was made to the array
    
    
        private int price;
        private String address;
        public final static int RNT = 0;
        public final static int CND = 1;
        public final static int SFD = 2;
        public final static int MAX_ROOMS = 20;
        static final int MAX_PRICE = 10000000;
        public final static int CONVERSION_FACTOR = 300;
        public final static int MAX_ADDY_LENGTH = 500;
        public final static int MAX_DAYS_ON_MARKET = 365 * 5; // 5 years
        public final static String UNDEFINED = "[ undefined ]";  // for undefined strings
        static final String PRICE = "ByPrice";
        public String GetAddress()
        {
           return address;
        }
        public boolean SetAddress(String address)
        {
           if (address.length() > 0 && address.length() < MAX_ADDY_LENGTH)
           {
              this.address = address;
              return true;
           }
           else
              return false;
        }
        public boolean SetPrice(int price)
        {
           if ( price > 0 && price <= MAX_PRICE)
           {
              this.price = price;
              return true;
           }
           else 
              return false;
        }
        public int GetPrice()
        {
           return price;
        }
        public Property()
        {
           price = 0;
           address = UNDEFINED;
        }
        public Property(int price,String address)
           {
              SetPrice(price);
              SetAddress(address);   
           }
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Ok, while this aproach could work, I'd suggest trying to do this OOP style. Methods like compareTo() should not be static, but made to work on an individual Property object, kinda like this:
    Java Code:
    public class Property implements Comparable{
      //definition and methods go here
      public int compareTo(Property p) {
         if(this.someProperty < p.someProperty)
           return -1;
         else if(this.someProperty > p.someProperty)
           return 1;
         return 0;
      }
    }
    This is of course just an example, there are much better ways to define compareTo(). Next, the FloatLargestToTop() shouldn't really be in the class Property. An object should know about itself, and how to compare itself to other Property objects, handling a group of those would be better left to another class, for instance PropertyGroup:
    Java Code:
    public class PropertyGroup {
      Property[] p;
      public PropertyGroup() {
        //construct the array of properties
      }
    
      public boolean FloatLargestToTop() {
        int maxIndex = 0;
        for(int i = 1; i < p.length; i++) {
          if(p[maxIndex].compareTo(p[i] == 1) //by my compareTo definition
            maxIndex = i;
        }
        if(maxIndex == 0) //largest element already on top
          return false;
        swap(0, maxIndex);
        return true;
      }
    
      private void swap(int index1, int index2) {
        Property temp = p[index1];
        p[index1] = p[index2];
        p[index2] = temp;
      }
    }
    The sooner you grasp and start to do things by OOP, the simpler Java will become. Also, for classes with multiple variables that could be used for comparison, take a look at the Comparator interface. In the rough, you make a class PropertyComparator implements Comparator, you implement the method public int compare(Object o1, Object o2) (Comparator can be generic), and you can have multiple such classes for each of the variables you want to compare. I had an assignment like this at uni, I solved it by making an abstract class that implemented Comparator, and based on user input would return the correct Comparator object for the variable I wanted to compare.
    Last edited by m00nchile; 04-25-2010 at 07:14 AM.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by hongi View Post
    I already figure it out how to sort by name but i can't sort by price because compareTo don't accept int.

    that's because compareTo accepts only objects. so i had an object called person which holds also the age of the person in it as int and i wanted to sort my list by age, so in my person class i used for compareTo method the following code

    Java Code:
    	public int compareTo (Person p) {
    		Integer i1 = this.age;
    		Integer i2 = p.age;
    		return i1.compareTo(i2);
    	}

    the approach is to convert your primitives to objects and then call the compareTo method.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Heh, I did go off on a tangent there :D. Anyways, to the OP, what j2me64 said answers your question perfectly... but try to take a few pointers from my post as well, it'll do you good in the long run.

  5. #5
    hongi is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  2. How to sort array objects into alphabetical order...
    By lisalala in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 11:01 AM
  3. Need help. Array sort
    By buzz1500 in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 04:24 AM
  4. Array sort
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-07-2008, 11:41 PM
  5. How to sort an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:48 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
  •