Output of int sort and method to sort Strings
My assignment asks me to make a TV show program, where I can input shows,
delete, modify and sort them. What I'm stuck on is the sorting part. With
the show, it asks for the name, day a new episode premieres, and time.
Those are the keys I need to sort it by.
The program prompts the user to input one of those keys, then the program
needs to sort (sorting by day will sort alphabetically).
I made a class and used an array. Here is the class:
public class showInfo
{
String name;
String day;
int time;
}
And the method to sort by time in the code:
public static void intSort()
{
int min;
for (int i = 0; i < arr.length; i++)
{
// Assume first element is min
min = i;
for (int j = i+1; j < arr.length; j++)
{
if (arr[j].time < arr[min].time)
{
min = j;
}
}
if (min != i)
{
int temp = arr[i].time;
arr[i].time = arr[min].time;
arr[min].time = temp;
}
}
System.out.println("TV Shows by Time");
for(int i = 0; i < arr.length; i++)
{
System.out.println(arr[i].name + " - " + arr[i].day + " - " +
arr[i].time + " hours");
}
}
When I call it and output it in the main, it only shows "TV Shows by Time"
and not the list. Why is this?
Also, I need to make ONE method that I will be able to use to sort both
the day AND the name (both Strings). How can I do this without using those
specific arrays (arr[i].name, arr[i].day) in the method?
No comments:
Post a Comment