When using a normal array, you can call Java.util.Arrays.sort () if you want to sort the data. However, to sort the arrays by this way, the objects in the array are also required to implement the Comparable<t> interface.
Packageorg.lyk.entities; Public classBookImplementsComparable<book>{ Static intTotalCount; PrivateString title; @Override PublicString toString () {return"Book [title=" + title + ", price=" + Price + "]"; } Private floatPrice ; PublicBook () {} PublicBook (String title,floatPrice ) { Super(); This. title =title; This. Price =Price ; } @Override Public intcompareTo (Book O) {//private String title;//private float price;//private String category;//private Author Author; if( This. Price <O.price)return-1; Else if( This. Price >O.price)return1; if( This. Title.compareto (o.title)! = 0) return This. Title.compareto (O.title); return0; } }
PackageOrg.lyk.main;ImportOrg.lyk.entities.Book; Public classmain{ Public Static voidMain (string[] args) {book[] books=Newbook[] {NewBook ("Java", 85), NewBook ("Oracle", 99), NewBook ("HTML5", 85), NewBook ("JSP", 64), NewBook ("Servlet", 99) }; Java.util.Arrays.sort (books); for(book item:books) {System.out.println (item); } System.out.println ((""instanceofString)); }}
The Comparable<t> interface in Java