0
public void sortTracksList()
{
    for(int j=0; j<tracksArray.length; j++)
    {
        for(int k =0; k<(tracksArray.length-1); k++)
        {

            if(tracksArray[k] > tracksArray[k+1])
            {
                int tempTracksArray = tracksArray[k];
                tracksArray[k] = tracksArray[k+1];
                tracksArray[k+1] = tempTracksArray;

            }
        }
    }

    for (int l = 0; l < tracksArray.length; l++)
    {
        System.out.println(tracksArray[l].toString());
    }
}

That's my code and it has 3 errors:

 //if(tracksArray[k] > tracksArray[k+1]) - bad operand types for binary operator
    //int tempTracksArray = tracksArray[k]; - Tracks cannot be converted to int
    //tracksArray[k+1] = tempTracksArray; - int cannot be converted to Tracks

Tracks class:

public class Tracks
{   
    int trackID;
    String trackTitle;
    String trackArtist;
    double trackLength;
    boolean trackOffline;


    public String toString()
    {

        String trackData = trackID + "," +trackTitle + "," + trackArtist + 
        "," + trackLength + "," + trackOffline;
        return trackData;
    }
}

Needs to be sorted by ID

Jess H
  • 1
  • 3
  • 3
    What is `tracksArray`? – Ry- Mar 07 '18 at 15:06
  • Tracks[] tracksArray = new Tracks[100]; – Jess H Mar 07 '18 at 15:08
  • 3
    you cannot compare objects with simple operators like `>`. That only works on primitive types like `int`, `long` etc. – OH GOD SPIDERS Mar 07 '18 at 15:11
  • You need to post your Tracks class to better understand how one Tracks object is to be compared to another Tracks object. – Ian Mc Mar 07 '18 at 15:16
  • 1
    For the error on `tracksArray[k] > tracksArray[k+1]` you should probably go read up on [Comparable](https://stackoverflow.com/questions/21626439/how-to-implement-the-java-comparable-interface). The other two errors are fixed by simply changing the type of `tempTracksArray`, although the error should make that obvious. – Bernhard Barker Mar 07 '18 at 15:40
  • How should it be sorted? By title, by id? – Ian Mc Mar 07 '18 at 16:24

1 Answers1

0

Your code has two issues.

  1. int tempTracksArray = tracksArray[k]; is incorrect. The tracksArray definition is Tracks[] tracksArray; the array holds objects of type Tracks. Therefore the correct temporary assignment is Tracks tempTracksArray = tracksArray[k];
  2. The > operator is not allowed on objects. Two approaches are shown below to solve this.

Option 1: Compare the integers

for(int j=0; j<tracksArray.length; j++)
{
    for(int k =0; k<(tracksArray.length-1); k++)
    {
        if(tracksArray[k].trackID > tracksArray[k+1].trackID)
        {
            Tracks tempTracksArray = tracksArray[k];
            tracksArray[k] = tracksArray[k+1];
            tracksArray[k+1] = tempTracksArray;

        }
    }
}

Option 2: Use the Comparable interface

for(int j=0; j<tracksArray.length; j++)
{
    for(int k =0; k<(tracksArray.length-1); k++)
    {
        if(tracksArray[k].compareTo(tracksArray[k+1]) > 0)
        {
            Tracks tempTracksArray = tracksArray[k];
            tracksArray[k] = tracksArray[k+1];
            tracksArray[k+1] = tempTracksArray;

        }
    }
}

In which case your Tracks class must implement Comparable:

class Tracks implements Comparable<Tracks> {
    int trackID;
    ......
    @Override
    public int compareTo(Tracks o) {
        return Integer.compare(this.trackID, o.trackID);
    }
}

The second option is preferred. Your Tracks class now has a natural method to describe how it should be sorted.

Ian Mc
  • 5,656
  • 4
  • 18
  • 25