0

I have an array of objects populated by a .txt file

Object[] punteggiTutti = scores.toArray();

Even if it's possible to use the .sort function:

Arrays.sort(punteggiTutti, Collections.reverseOrder());

I would know how to apply a working bubblesort algorithm; I tried the following not working code:

for(int i=0; i<j; i++) 
    {
    if(punteggiTutti[i]<punteggiTutti[i+1])  // error "<" operator cannot be used in objects 
      {
      temp=punteggiTutti[i]; 
      punteggiTutti[i]=punteggiTutti[i+1];
      punteggiTutti[i+1]=temp;
      }
    }
6
  • < cannot be used in java.lang.Object Commented Mar 13, 2011 at 21:13
  • Do you understand what the error means? Commented Mar 13, 2011 at 21:25
  • Of course I do!!! So the answer to my question is simply... not possible?! :) Commented Mar 13, 2011 at 21:28
  • it's an array created from a .txt file In the first file i have team scores In the second file i have the name of teams. I need bubble sort to create a unique index which can sort both of the arrays created from the 2 files Any ideas? Commented Mar 14, 2011 at 0:22
  • You need to figure out how to compare your objects. Commented Mar 14, 2011 at 0:26

2 Answers 2

5

You can't compare two Objects with < operator.

Use something which implements Comparable interface. Then such elements can be compared using function a.compareTo(b);

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Comparable.html

Also have a look at: http://leepoint.net/notes-java/data/expressions/22compareobjects.html

Try to read about equality in Java. This should help you with future errors when using Java.

Sign up to request clarification or add additional context in comments.

Comments

0

Create an Comparator for your object, to compare on object against the other. The built in mergeSort algorithm is much faster than bubblesort.

http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/sortcontest/sortcontest.htm http://www.sorting-algorithms.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.