如何在java中对包含多个字段的类数组进行排序

时间:2014-06-27 20:12:29

标签: java arrays sorting

我有这个代码,要求用户提供书名,作者和页数,并将它们存储在类数组中。然后它询问用户他们希望如何排序书籍。代码有效,但我的教授说它可以更有效地进行排序。不幸的是,在我的第二个java课程之前,我的书并没有超过比较器方法或集合,因此我无法使用它们。我需要做到这一点,以减少重复,但我完全没有想法。

这是当前的代码:

import javax.swing.*;
public class LibraryBookSort
{
   public static void main(String[] args)
   {
      String bookTitle, bookAuthor, bookPC, sortOption;
      String temp = "", temp2 = "", temp3 = "";
      int a, b, x;
      LibraryBook[] book = new LibraryBook[5];
      for(x = 0; x < book.length; ++x)
      {
         bookTitle = JOptionPane.showInputDialog(null, "Please enter a book title:");
         bookAuthor = JOptionPane.showInputDialog(null, "Please enter the book's author:");
         bookPC = JOptionPane.showInputDialog(null, "Please enter the book's page count:");
         book[x] = new LibraryBook();
         book[x].setTitle(bookTitle);
         book[x].setAuthor(bookAuthor);
         book[x].setPageCount(bookPC);
      }
      sortOption = JOptionPane.showInputDialog(null, "How would you like the books to be sorted?"
         + "\nPress 1 to sort by title \nPress 2 to sort by author \nPress 3 to sort by page count");
      int comparisonsToMake = book.length - 1;
      switch(sortOption)
      {
         case "1":            
            for(a = 0; a < book.length - 1; ++a)
            {
               for(b = 0; b < comparisonsToMake; ++b)
               {
                  if(book[b].getTitle().compareTo(book[b + 1].getTitle()) > 0)
                  {
                     temp = book[b + 1].getTitle();
                     temp2 = book[b + 1].getAuthor();
                     temp3 = book[b + 1].getPageCount();
                     book[b + 1].setTitle(book[b].getTitle());
                     book[b + 1].setAuthor(book[b].getAuthor());
                     book[b + 1].setPageCount(book[b].getPageCount());
                     book[b].setTitle(temp);
                     book[b].setAuthor(temp2);
                     book[b].setPageCount(temp3);
                  }
               }
               --comparisonsToMake;
            }
            break;
         case "2":
            for(a = 0; a < book.length - 1; ++a)
            {
               for(b = 0; b < comparisonsToMake; ++b)
               {
                  if(book[b].getAuthor().compareTo(book[b + 1].getAuthor()) > 0)
                  {
                     temp = book[b + 1].getTitle();
                     temp2 = book[b + 1].getAuthor();
                     temp3 = book[b + 1].getPageCount();
                     book[b + 1].setTitle(book[b].getTitle());
                     book[b + 1].setAuthor(book[b].getAuthor());
                     book[b + 1].setPageCount(book[b].getPageCount());
                     book[b].setTitle(temp);
                     book[b].setAuthor(temp2);
                     book[b].setPageCount(temp3);
                  }
               }
               --comparisonsToMake;
            }
            break;
         case "3":            
            for(a = 0; a < book.length - 1; ++a)
            {
               for(b = 0; b < comparisonsToMake; ++b)
               {
                  if(book[b].getPageCount().compareTo(book[b + 1].getPageCount()) > 0)
                  {
                     temp = book[b + 1].getTitle();
                     temp2 = book[b + 1].getAuthor();
                     temp3 = book[b + 1].getPageCount();
                     book[b + 1].setTitle(book[b].getTitle());
                     book[b + 1].setAuthor(book[b].getAuthor());
                     book[b + 1].setPageCount(book[b].getPageCount());
                     book[b].setTitle(temp);
                     book[b].setAuthor(temp2);
                     book[b].setPageCount(temp3);
                  }
               }
               --comparisonsToMake;
            }
            break;
         default:
            JOptionPane.showMessageDialog(null, "That is an invalid option."
               + "\nThe books will not be sorted");
            break;
      }
      for(x = 0; x < book.length; ++x)
         book[x].display();      
   }
}

有没有人有任何建议?

2 个答案:

答案 0 :(得分:0)

如果&#34;可以更有效地排序&#34;意味着更少的代码,封装

                 temp = book[b + 1].getTitle();
                 temp2 = book[b + 1].getAuthor();
                 temp3 = book[b + 1].getPageCount();
                 book[b + 1].setTitle(book[b].getTitle());
                 book[b + 1].setAuthor(book[b].getAuthor());
                 book[b + 1].setPageCount(book[b].getPageCount());
                 book[b].setTitle(temp);
                 book[b].setAuthor(temp2);
                 book[b].setPageCount(temp3);

作为一个功能

如果&#34;可以更有效率&#34;在排序意味着复杂性,使用不同的排序算法

答案 1 :(得分:0)

看起来您正在使用bubblesort算法,它具有O(n ^ 2)平均大小写复杂度。我建议将其替换为具有O(nlogn)平均案例复杂度的Quicksort。