我這個程序,它應該按照從最大到最小的順序排列電影列表,通過查看發佈年份。這裏是方法,我也有打印它的方法。它以abc順序而不是5,4,3,2,1順序排列。我的程序沒有按照我想要的排序
System.out.println("Sorted by Year - descending:");
sortYears(myMovies,0,myMovies.length-1);
printMovies(myMovies);
public static void sortYears(Movie4[] myMovies , int low, int high)
{
if(low >= high)
return;
int mid = (low + high)/2;
sortYears(myMovies, low, mid);
sortYears(myMovies, mid+1, high);
mergYears(myMovies, low, mid, high);
}
public static void mergYears(Movie4[] myMovie, int low, int mid, int high)
{
int tempLow = low;
int tempMid = mid;
int indexCnt =0;
while(tempLow < mid || tempMid < high)
{
if(tempLow > mid)
{
myMovie[indexCnt].equals(myMovie[tempMid]);
tempMid++;
}
else if(tempMid > high)
{
myMovie[indexCnt].equals(myMovie[tempLow]);
tempLow++;
}
else if(myMovie[tempLow].getYear() < myMovie[tempMid].getYear())
{
myMovie[indexCnt].equals(myMovie[tempLow]);
tempLow++;
}
else
{
myMovie[indexCnt].equals(myMovie[tempMid]);
tempMid++;
}
indexCnt++;
}
for(int x = low; x < high; x++)
{
myMovie[x].equals(myMovie[x-low]);
}
}
電影4:
public class Movie4 {
// instance variables
String title ;
int year;
String studio;
/**
* Constructor for objects of class InventoryItem
*/
public Movie4(String t,int y,String s)
{
// initialise instance variables
title = t;
year = y;
studio = s;
}
public String getTitle()
{
return title;
}
public int getYear()
{
return year;
}
public String getStudio()
{
return studio;
}
@Override
public String toString()
{
return title + ", " + year + ", "+studio;
}
public boolean equals (Movie4 other)
{
return(title.equals(other.getTitle()));
}
public int compareTo(Object other)
{
int result;
String otherTitle = ((Movie4)(other)).getTitle();
result = title.compareTo(otherTitle);
return result;
}
}
此處缺少一些內容:「Movie4」的代碼。提示:還有'Arrays.sort()'。 – fge
@fge我可能是錯的,但我正在得到OP的想法來實現他/她自己的分揀系統的基礎上仔細考慮使用合併排序的氛圍。 – supersam654
我應該在這裏發佈Movie4代碼嗎? – user2443602