PDA

Click to See Complete Forum and Search --> : Sorting Problem (Urgent)


Ramandeep
Apr 21st, 2001, 07:41 AM
Hi, I've just written a sort method for my top ten songs chart program, which just hold a list of the top ten songs.

Each records is made up off the following fields:

Chart Position :int
Song Title :string
Song Artist :string
Record Company :string
Price :double

The method sorts records by Chart Position fine, but when I try to sort by Song Artist and re compile it gives me a error like the following error:

"cannot apply > to string" or somthing.

How can I compaire the strings to see which is greater, I want to sort Song Artist alphabeticly.
Here is how I'm sorting by Chart Position (I'm using bubble sort as you cannot have more than 10 records).


passes = 1;
swap = true;

while ( ( passes <= arrayToSort.length - 1 ) && ( swap = true ) )
{
i = 0;
swap = false;

while ( i <= arrayToSort.length - 2 )
{
if ( arrayToSort[i].chartPosistion > arrayToSort[i + 1].chartPosistion )
{
tempRecord.chartPosistion = arrayToSort[i].chartPosistion;
tempRecord.songTitle = arrayToSort[i].songTitle;
tempRecord.songArtist = arrayToSort[i].songArtist;
tempRecord.recordCompany = arrayToSort[i].recordCompany;
tempRecord.price = arrayToSort[i].price;

arrayToSort[i].chartPosistion = arrayToSort[i + 1].chartPosistion;
arrayToSort[i].songTitle = arrayToSort[i + 1].songTitle;
arrayToSort[i].songArtist = arrayToSort[i + 1].songArtist;
arrayToSort[i].recordCompany = arrayToSort[i + 1].recordCompany;
arrayToSort[i].price = arrayToSort[i + 1].price;

arrayToSort[i + 1].chartPosistion = tempRecord.chartPosistion;
arrayToSort[i + 1].songTitle = tempRecord.songTitle;
arrayToSort[i + 1].songArtist = tempRecord.songArtist;
arrayToSort[i + 1].recordCompany = tempRecord.recordCompany;
arrayToSort[i + 1].price = tempRecord.price;

swap = true;
}

i = i + 1;
}

passes = passes + 1;
}


To sort by Song Artist

I did the same as above but changing the if from:


if ( arrayToSort[i].chartPosistion > arrayToSort[i + 1].chartPosistion )


to:


if ( arrayToSort[i].songArtist > arrayToSort[i + 1].songArtist )

Dillinger4
Apr 21st, 2001, 12:48 PM
Use the one of the various sort methods from
java.util.Arrays there are many and the one you
probably need is

public static void sort(Object[] a);

Since Strings are objects in java i would
assume that you can fill up an Object array
with the song artists and apply this method to
the array

Dillinger4
Apr 21st, 2001, 12:56 PM
Im just curious.... it looks like you are storing
the chart position's and the song Artist's in the same
array. So is arrayto sort a string array? And are chart
position and Song artist string types?

You should also use a Vector "java.util.Vector; which
is similar to a Collection in VB. This way you program
wont be limited buy the amount of artists it can store.




if ( arrayToSort[i].chartPosistion > arrayToSort[i + 1].chartPosistion )


if ( arrayToSort[i].songArtist > arrayToSort[i + 1].songArtist )

Ramandeep
Apr 21st, 2001, 04:19 PM
Yes they are stored in the same array. The array elements are objects of class SongAccount that is defined as follows:

class SongAccount
{
int chartPosistion;

String songTitle,
songArtist,
recordCompany;

double price;
}

I'm mimicking the record effect that other languages usually provide; you know where you can define your own data type.

About the vector thing, I could use it and I would in other circumstances but the assignment specification only say’s the program has to manage 10 song records.

Dillinger4
Apr 21st, 2001, 04:24 PM
So i guess since your array is storing objects of your
class you should be able to use the
public static void sort(Object[] a); method. Try it
out and tell us if it works

Ramandeep
Apr 24th, 2001, 01:47 PM
OK I just handed my coursework in yesterday and I completed the programs and it's working in tip top order.

I also sorted out that sorting problem I had, I did the following


if ( arrayToSort[i].songArtist.compareTo ( arrayToSort[i + 1].songArtist ) > 0 )


I didn't try that last suggestion you made Dilenger4 as I had worked the problem before I saw it, but I would like to thank you and the other guys for all the help, really appreciated.

:) !!! THANKS GUYS !!! :)