calling an array to sort...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShaveDave27
    New Member
    • Apr 2007
    • 22

    #1

    calling an array to sort...

    hi,
    i've now created a sorting algorithm which i am happy with. But i cant get it to sort between to Person Objects.

    Heres the code:
    Code:
    public class main1
    {
        public static void insertionSort(Comparable [] a)
        {
            int j;
            for (int i = 0; i<a.length; i++)
            {
                Comparable Person = a[i];
                for ( j = i; j>0; j--);
                {
                    if (a[j-1].compareTo (a) <0)
                    {
                        break;
                    }
                    else
                    {
                        a[j] = a[j-1];
                    }
                }
                a[j] = Person;
                
                
            }
        }
    }
    the variable a is the array, i have created an array called people but when i input people instead of a it doesnt call my array people - it just creates the variable people.

    This is really annoying me!!

    Any help would be great, thanks

    Dave
    Last edited by JosAH; May 2 '07, 02:23 PM. Reason: added [code] ... [/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by ShaveDave27
    the variable a is the array, i have created an array called people but when i input people instead of a it doesnt call my array people - it just creates the variable people.
    I'm sorry; I've read that sentence over and over again but I do not understand
    what it's all about. Could you elaborate a bit please?

    kind regards,

    Jos

    ps. have you read one of the"Tip of the week"s? It's all about sorting and
    abstraction from arrays and whatever.

    Comment

    • merinodaniel
      New Member
      • May 2007
      • 5

      #3
      i dont know if its ok to compare a object in a array with all the array in this line :

      if (a[j-1].compareTo (a) <0)

      i try to do this code but i can figure it out how to do this line, sry i thinks its wrong this compareTo.
      bye

      Comment

      • prometheuzz
        Recognized Expert New Member
        • Apr 2007
        • 197

        #4
        Originally posted by ShaveDave27
        hi,
        i've now created a sorting algorithm which i am happy with. But i cant get it to sort between to Person Objects.

        Heres the code:
        Code:
        // ...
                    for ( j = i; j>0; j--);
                    {
                        if (a[j-1].compareTo (a) <0)
                        {
                            // ...
        ...
        That semi colon looks fishy after your for-statement.

        Comment

        Working...