如何对对象数组中的双精度值排序

时间:2019-07-19 20:31:43

标签: java arrays object primitive-types

我有一个目前坚持的作业。我有一个SchoolTextBook类,其中包括每个数据成员的获取器/设置器。我必须在SchoolTextBook排序类中创建对象,将它们加载到数组中,并询问用户他们希望如何对信息进行排序。除价格数据成员外,我还进行了其他所有排序。我一辈子都无法弄清楚如何从数组中的对象中拉出double值,并根据该值对数组的内容进行排序。我的int PageCount也有类似的问题。

我已经尝试了Array中的sort方法。但是我找不到正确比较双精度值的方法。

公共课SchoolTextBookSort {

public static void main(String [] args)
{
    char choice;
    SchoolTextBook[] books = new SchoolTextBook[5];

    SchoolTextBook b1 = new SchoolTextBook("Android Bootcamp", "Corinne Hoisington", 500, "9781305857995", 53.87);

    SchoolTextBook b2 = new SchoolTextBook("Linux Essentials", "Christine Bresnahan", 342, "9871119092063", 31.93);

    SchoolTextBook b3 = new SchoolTextBook("Murach's MySQL", "Joel Murach", 590, "9781890774820", 57.50);

    SchoolTextBook b4 = new SchoolTextBook("Java Programming", "Joyce Farrell", 994, "9781285856919", 52.00);

    SchoolTextBook b5 = new SchoolTextBook("C# Programming", "Barbara Doyle", 1156, "9781285856872", 91.69);

    books[0] = b1;
    books[1] = b2;
    books[2] = b3;
    books[3] = b4;
    books[4] = b5;

    greeting();
    choice = getChoice();

    switch(choice)
    {
    case 'A':
        sortTitle(books);
        break;
    case 'B':
        sortAuthor(books);
        break;
    case 'C':
        sortPages(books);
        break;
    case 'D':
        sortPrice(books);
        break;
    case 'E':
        sortISBN(books);
        break;
    default:
        System.out.println("The books we currently have on-hand:");
        System.out.println(b1.toString() + "\n" + b2.toString() + "\n" 
                    + b3.toString() + "\n" + b4.toString() + "\n" + b5.toString());
        break;
    }

}

public static void greeting()
{
    System.out.println("School Textbook Program\n\n");
}

public static char getChoice()
{
    String choice = "";
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the character corresponsing to the type of sort you want:");
    System.out.println("A: Alphabetically by title\nB: Alphabetically by Author" 
    + "\nC: Least to most pages\nD: Book price -- low to high\nE: ISBN");
    System.out.print("\nPlease enter your choice: ");
    choice = input.nextLine();

    choice = choice.toUpperCase();

    char character = choice.charAt(0);

    return character;
}

public static void sortTitle(SchoolTextBook arr1[])
{
    Arrays.sort(arr1, (a, b) -> a.getTitle().compareTo(b.getTitle()));
    System.out.println(arr1);
}

public static void sortAuthor(SchoolTextBook arr1[])
{
    Arrays.sort(arr1, (a, b) -> a.getAuthor().compareTo(b.getAuthor()));
    System.out.println(arr1);
}

public static void sortPages(SchoolTextBook arr1[])
{
    Arrays.sort(arr1, (a, b) -> a.getPageCount() - b.getPageCount());

}

public static void sortISBN(SchoolTextBook arr1[])
{
    Arrays.sort(arr1, (a, b) -> a.getISBN().compareTo(b.getISBN()));    
    System.out.println(arr1);
}

public static void sortPrice(SchoolTextBook arr1[])
{
    //Tried something like the sortPages method but
   //it didn't work for either method

}

0 个答案:

没有答案