8 Replies - 341 Views - Last Post: 06 December 2012 - 09:55 AM Rate Topic: -----

#1 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Sorting Java Array problem?

Posted 03 December 2012 - 03:45 AM

Hi guys,

We are meant to create a method called sortTheArray (self explanatory)...

We are not allowed to use java.util.Arrays according to the specification even though its much easier... We have to recreate simple version of vector class hence need to write our own sorting method..

this is what I have so far...


	public void sortTheArray()
	{	
		//create a new array for holding the values of the old array.
		for (int i = 0; i < itemArray.length; i++)
		{
			for (int j = i + 1; j < itemArray.length; j++)
			{
				[b]if (itemArray[i] > itemArray[j])[/b]
				{
					Item tempValue = itemArray[j];
					itemArray[j] = itemArray[i];
					itemArray[i] = tempValue;
				}
				
			}
		}




I get the following error with the if statement..

The operator
 > 
is undefined for the argument type(s) unisa.progfund.item, unisa.progfund.item :S

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Java Array problem?

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Sorting Java Array problem?

Posted 03 December 2012 - 03:57 AM

You cannot use relational operators on objects. Perhaps you meant itemArray[i].somePrimitive or .someMethod().
Was This Post Helpful? 0
  • +
  • -

#3 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Re: Sorting Java Array problem?

Posted 03 December 2012 - 04:26 AM

View PostMylo, on 03 December 2012 - 03:57 AM, said:

You cannot use relational operators on objects. Perhaps you meant itemArray[i].somePrimitive or .someMethod().



Makes more sense now...
how do i use primitive data type to compare two objects?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sorting Java Array problem?

Posted 03 December 2012 - 04:39 AM

Your class as to implement the Comparable interface and thus the comparateTo() method that returns < 0 or == 0 or > 0 depending if comparable > < or =

class Number implements Comparable<Number> {

    private int value;

    public int compareTo(Number another) {
        if(value > another.value)
           return +1;
        if(value < another.value)
           return -1;
        return 0;          // the 2 are equals
    }

// or more simply vecause we compare int

    public int compareTo(Number another) {
        return value - another.value;
    }



then in your sort
     int diff = item[i].compareTo(item[j]);
     if(diff < 0)
       ..swap them



Happy coding
Was This Post Helpful? 1
  • +
  • -

#5 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Re: Sorting Java Array problem?

Posted 03 December 2012 - 04:44 AM

thank you pbl...
i will give that a shot right now.. thanks again.
Was This Post Helpful? 0
  • +
  • -

#6 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Re: Sorting Java Array problem?

Posted 06 December 2012 - 01:04 AM

hi guys..

i am still working with the sortTheArray method.. it works, we sorta.. this is the code:


	public void sortTheArray()
	{
		//iterates through number of items minus 1
		for (int i = 0; i < numberOfItems - 1; i++)
		{
			for (int j = i + 1; j < numberOfItems; j++)
			{
				//itemArray[i] gets the key
				//and compares it to the next index in the array
				//and checks to ensure the value is greater than zero
				if(itemArray[i].getKey().compareTo(itemArray[i+1].getKey()) > 0)
				{
					//swaps the items in the array.
					Item tempValueHolder = itemArray[j];
					itemArray[j] = itemArray[i];
					itemArray[i] = tempValueHolder;
				}

			}
		}
		sorted = true;
	}



i was provided with feedback for this code saying: only swap positions if you have found index of next smallest. I donít think this code finds the next smallest but instead next number that is larger not smaller.

i am just a bit unsure on how do we find the next smallest instead of the next number?
Was This Post Helpful? 0
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Sorting Java Array problem?

Posted 06 December 2012 - 03:08 AM

You should use your "j" variable as in:

import java.util.Arrays;

public class Item implements Comparable<Item>
{
    private int id;
    private int key;
    
    public Item(int id, int key){
        this.id = id;
        this.key = key;
    }
    
    @Override
    public int compareTo(Item other){
        return key - other.key;
    }
    
    @Override
    public String toString(){
        return String.format("ID: %d\tKey: %d\n", id, key);
    }
    
    public int getKey(){
        return key;
    }
    
    public int getId(){
        return id;
    }
    
    
}


class MainClass
{
    public static void main(String[] args){
        Item[] items = {new Item(1,10), new Item(2,15), new Item(3,12), new Item(4,4),
            new Item(5,1), new Item(6,3), new Item(7,1000), new Item(8,45)};
        
        System.out.println(Arrays.toString(items) +"\n\n");
        sortTheArray(items);
        System.out.println(Arrays.toString(items));
        
    }
    
    
    public static void sortTheArray(Item[] items){
        int len = items.length;
        
        for(int i=0; i < len; i++){
            for(int j=i + 1; j < len; j++){
                if(items[i].compareTo(items[j]) > 0){
                    //swap
                    Item temp = items[i];
                    items[i] = items[j];
                    items[j] = temp;
                }
            }
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#8 theNoob  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 14-July 12

Re: Sorting Java Array problem?

Posted 06 December 2012 - 03:13 AM

View Postdarek9576, on 06 December 2012 - 03:08 AM, said:

You should use your "j" variable as in:

import java.util.Arrays;

public class Item implements Comparable<Item>
{     
        for(int i=0; i < len; i++){
            for(int j=i + 1; j < len; j++){
                if(items[i].compareTo(items[j]) > 0){
                    //swap
                    Item temp = items[i];
                    items[i] = items[j];
                    items[j] = temp;
                }
            }
        }
    }
}



You are a genius :D thank you so much.. i swapped the "i" and "j"s and it works :D thank you so much. I really appreciate it. :)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Sorting Java Array problem?

Posted 06 December 2012 - 09:55 AM

Also you should use a variable that holds if a swap was done or not
If you have an iteration where no sxwap occured, no need to continue

baavgai has a tutorial about that

http://www.dreaminco...al-bubble-sort/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1