我如何自定义排序在Java中的数组对象按字母顺序排列? [英] How do I sort custom objects alphabetically in an array in Java?

查看:93
本文介绍了我如何自定义排序在Java中的数组对象按字母顺序排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何按字母顺序排序我的广告资源数组?

这是为我工作的一个项目。我试图用 Arrays.sort(库存); ,但没有奏效。

可能有人请告诉我,我做错了?

图书类:

  //文件名:BookStore.java
//说明:目的是为了显示有库存,ISBN,书名,作者名,年份,发行商名称和价格什么书。
//作者姓名:大卫Finocchi
//日期:2014年1月19日
进口java.util.Arrays中;
进口java.text.DecimalFormat中;类Book {
    保护长ISBN;
    受保护的字符串称号;
    保护字符串autName;
    保护INT年;
    保护字符串pubName;
    保护双重价格;
    保护INT数量;
    //构造
    公共图书(长ISBN,标题字符串,字符串autName,年整型,字符串pubName,双价){
        setIsbn(ISBN);
        的setTitle(职称);
        setAut(autName);
        setYear(年);
        setPub(pubName);
        setPrice(价);
    }    对于类变量//方法
    众长getIsbn(){
        返回ISBN;
    }    公共无效setIsbn(长ISBN){
        this.isbn = ISBN;
    }    公共字符串的getTitle(){
        返回称号;
    }    公共无效的setTitle(字符串名称){
        this.title =称号;
    }    公共字符串getAut(){
        返回autName;
    }    公共无效setAut(字符串autName){
        this.autName = autName;
    }    公众诠释了getYear(){
        返回年;
    }    公共无效setYear(INT年){
        this.year =年;
    }    公共字符串getPub(){
        返回pubName;
    }    公共无效setPub(字符串pubName){
        this.pubName = pubName;
    }    公共双用getPrice(){
        返回的价格;
    }    公共无效setPrice(双价){
        this.price =价格;
    }    公众诠释getQuantity(){
        返回数量;
    }    公共无效setQuantity(INT数量){
        this.quantity =数量;
    }    公共双calTotal(){
        返回的价格;
    }    //为的toString书阵列
    公共字符串的toString(){
        回归ISBM:+ ISBN +\\ n+标题:+标题+\\ n+作者姓名:+ autName +\\ n+出版年份+一年+\\ n +发行者的名称:+ pubName +\\ n+价格:$+价格+\\ n \\ n;
    }} //结束类图书

电子书类:

 类电子书扩展书{    私人字符串网站;    //构造函数和超
    公共电子书(长ISBN,标题字符串,字符串autName,年整型,
                 串pubName,双价,弦乐网站){
        超(ISBN,标题,autName,今年,pubName,价格);
        setWebsite(网站);
    }    //获取和设置
    公共字符串getWebsite(){
        返回网站;
    }
    公共无效setWebsite(字符串网站){
        this.website =网站;
    }    公共双重折扣(){
        价格收益0.10 *;
    }    DecimalFormat的钱=新的DecimalFormat($ 0.00);    //的toString书阵列
    公共字符串的toString(){
        回归ISBM:+ ISBN +\\ n+标题:+标题+\\ n+作者姓名:+ autName +\\ n+出版年份+一年+\\ n +发行者的名称:+ pubName +\\ n+价格:+ money.format(价格)+\\ n+网站:+网站+\\ n+折扣:+钱。格式(折扣())+\\ n \\ n;
    }} //结束类电子书

图书城类(主要方法):

 公共类图书城{
    //主要方法
    公共静态无效的主要(字串[] args){
        // book数组
        书[]库存=新集[5];        电子书A =新的电子书(75260012L,大卫去学校,戴维香,2010年,香石,11.98HTTP://www.tinyurl.qqwert67o9);
        图书B =新的图书(7423540089L,没有大卫!,大卫·香,2009年,香石,12.99);
        图书C =新的图书(743200616L,简单富足,莎拉布雷斯纳克,2009年斯克里布纳,14.99);
        电子书D =新的电子书(78137521819L,非常饥饿的毛毛虫,埃里克·卡尔,2005年菲洛梅拉书,13.99HTTP://www.tinyurl.fguopt8u90);
        图书E =新的图书(9781416987116L,我们将在猎熊,迈克尔·罗森,2009年,McElderry,15.99);        盘点[0] = A;
        盘点[1] = B;
        盘点[2] = C;
        盘点[3] = D组;
        盘点[4] = E;        双总= 0.0;        //打印文本用户
        的System.out.println(书店的项目:+\\ n);
        //打印阵列
        的for(int i = 0; I< inventory.length;我++){
            的System.out.println(盘点[I]);
            总+ =盘点[I] .getPrice();
        }
        //打印总
        DecimalFormat的钱=新的DecimalFormat($ 0.00);
        System.out.printf(总库存值:+ money.format(总));
    }} //结束类图书城


解决方案

<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort%28java.lang.Object%5b%5d%29\"相对=nofollow> Arrays.sort()使用对象的自然排序,因此这将意味着你需要他们实施可比,否则你会得到不可靠的分拣行为。

简而言之,如果你想每本书通过标题进行排序,然后把它落实可比

 公共类图书实现可比&LT;图书&GT; {
    公众诠释的compareTo(图书otherBook){
        返回title.compareTo(otherBook.getTitle());
    }
}

您想也想为子做同样的电子书以及

How would I sort my inventory array alphabetically?

This is for a project that I am working on. I tried to use Arrays.sort(inventory); but it did not work.

Could someone please tell me what I am doing wrong?

Book class:

//Filename: BookStore.java
//Description: Purpose is to display what books are in stock and ISBN, title, author name, year, publishers name, and price.
//Author Name: David Finocchi
//Date: 01/19/2014
import java.util.Arrays;
import java.text.DecimalFormat;

class Book {
    protected long isbn;
    protected String title;
    protected String autName;
    protected int year;
    protected String pubName;
    protected double price;
    protected int quantity;


    // Constructor
    public Book(long isbn, String title, String autName, int year, String pubName, double price) {
        setIsbn(isbn);
        setTitle(title);
        setAut(autName);
        setYear(year);
        setPub(pubName);
        setPrice(price);
    }

    // methods for class variables
    public long getIsbn() {
        return isbn;
    }

    public void setIsbn(long isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAut() {
        return autName;
    }

    public void setAut(String autName) {
        this.autName = autName;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getPub() {
        return pubName;
    }

    public void setPub(String pubName) {
        this.pubName = pubName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double calTotal() {
        return price;
    }

    //toString for the book array
    public String toString() {
        return "ISBM: " + isbn + "\n" + "Title: " + title + "\n" + "Author's Name: " + autName + "\n" + "Year Published: " + year + "\n" + "Publisher's Name: " + pubName + "\n" + "Price: $" + price + "\n\n";
    }

}// end class Book

EBook class:

class EBook extends Book {

    private String website;

    //constructor and super
    public EBook(long isbn, String title, String autName, int year,
                 String pubName, double price, String website) {
        super(isbn, title, autName, year, pubName, price);
        setWebsite(website);
    }

    // get and set
    public String getWebsite() {
        return website;
    }


    public void setWebsite(String website) {
        this.website = website;
    }

    public double discount() {
        return price * 0.10;
    }

    DecimalFormat money = new DecimalFormat("$0.00");

    //toString for the Book array
    public String toString() {
        return "ISBM: " + isbn + "\n" + "Title: " + title + "\n" + "Author's Name: " + autName + "\n" + "Year Published: " + year + "\n" + "Publisher's Name: " + pubName + "\n" + "Price: " + money.format(price) + "\n" + "Website: " + website + "\n" + "Discount: " + money.format(discount()) + "\n\n";
    }

}//end class EBook

BookStore class (with main method):

public class BookStore {
    //main method
    public static void main(String[] args) {
        // book array
        Book[] inventory = new Book[5];

        EBook a = new EBook(75260012L, "David goes to School", "David Shannon", 2010, "Shannon Rock", 11.98, "http://www.tinyurl.qqwert67o9");
        Book b = new Book(7423540089L, "No David!", "David Shannon", 2009, "Shannon Rock", 12.99);
        Book c = new Book(743200616L, "Simple Abundance", "Sarah Breathnach", 2009, "Scribner", 14.99);
        EBook d = new EBook(78137521819L, "The very hungry caterpillar", "Eric Carle", 2005, "Philomel Books", 13.99, "http://www.tinyurl.fguopt8u90");
        Book e = new Book(9781416987116L, "We're going on a bear hunt", "Michael Rosen", 2009, "McElderry", 15.99);

        inventory[0] = a;
        inventory[1] = b;
        inventory[2] = c;
        inventory[3] = d;
        inventory[4] = e;

        double total = 0.0;

        //print text to user
        System.out.println("Bookstore Items:" + "\n");
        //print array
        for (int i = 0; i < inventory.length; i++) {
            System.out.println(inventory[i]);
            total += inventory[i].getPrice();
        }
        //print total
        DecimalFormat money = new DecimalFormat("$0.00");
        System.out.printf("Total Inventory Value: " + money.format(total));
    }

}// end class BookStore

解决方案

Arrays.sort() uses the natural ordering of the objects, so that would imply that you need them to implement Comparable, or you're going to get unreliable sorting behavior.

Simply put, if you want to sort each book by its title, then have it implement Comparable:

public class Book implements Comparable<Book> {
    public int compareTo(Book otherBook) {
        return title.compareTo(otherBook.getTitle());
    }
}

You'd also want to do the same for the subclass EBook as well.

这篇关于我如何自定义排序在Java中的数组对象按字母顺序排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆