Java Custom Sorting

Source: Internet
Author: User

1  Packagegoodssorting;2 //Instance Class3  Public classGoodsparameter {4     //Price5     Private DoublePrice ;6     //Collection Amount7     Private intfavor;8     //Product Name9     PrivateString Goodsname;Ten     //non-parametric construction One      PublicGoodsparameter () { A         Super(); -     } -     //with parametric construction the      PublicGoodsparameter (String Goodsname,DoublePriceintfavor) { -         Super(); -          This. Price =Price ; -          This. Favor =favor; +          This. Goodsname =Goodsname; -     } +      Public DoubleGetPrice () { A         returnPrice ; at     } -      Public voidSetprice (DoublePrice ) { -          This. Price =Price ; -     } -      Public intGetfavor () { -         returnfavor; in     } -      Public voidSetfavor (intfavor) { to          This. Favor =favor; +     } -      PublicString Getgoodsname () { the         returnGoodsname; *     } $      Public voidsetgoodsname (String goodsname) {Panax Notoginseng          This. Goodsname =Goodsname; -     } the     //overriding ToString + @Override A      PublicString toString () { the         return"\ t Goods:" + getgoodsname () + "\ t Price:" + getprice () + "\ T collection:" ++ getfavor () + "\ n"; -     } $}
1  Packagegoodssorting;2 //Sort Tool Class3 ImportJava.util.Comparator;4 5  Public classPricesortImplementsComparator<goodsparameter> {6 7      Public intCompare (Goodsparameter O1, Goodsparameter O2) {8         intI=0;9         //Sort by Price positive orderTen         if((O1.getprice ()-O2.getprice ()) > 0) { OneI=1; A          -         } -         Else { theI=-1; -              -         } -         returni; +          -     } +  A}
1  Packagegoodssorting;2 3 ImportJava.util.Comparator;4 5  Public classFavorsortImplementsComparator<goodsparameter> {6 7      Public intCompare (Goodsparameter O1, Goodsparameter O2) {8         inti = 0;9         //Sort by Favorites Positive orderTen         if(O1.getfavor ()-o2.getfavor () <0) {//reverse order only changes to >0 OneI=-1; A         } -         returni; -     } the  -}
1  Packagegoodssorting;2 3 ImportJava.util.Comparator;4 5  Public classNamesortImplementsComparator<goodsparameter> {6      Public intCompare (Goodsparameter O1, Goodsparameter O2) {7         intresult =o1.getgoodsname (). CompareTo (O2.getgoodsname ());8         returnresult;9     }Ten  One}
1  Packagegoodssorting;2 //Test Class3 Importjava.util.ArrayList;4 Importjava.util.Collections;5 Importjava.util.List;6 7  Public classTestApp {8      Public Static voidMain (string[] args) {9list<goodsparameter> list =NewArraylist<goodsparameter>();TenList.add (NewGoodsparameter ("iphone", 5200, 110000)); OneList.add (NewGoodsparameter ("Xiaomi", 1999, 50000)); AList.add (NewGoodsparameter ("Huawei", 1899, 60054)); -List.add (NewGoodsparameter ("Samsung", 4500, 80000)); -List.add (NewGoodsparameter ("Meizu", 1699, 125863)); the         //before sorting -System.out.println ("Original order: **********************"); - System.out.println (list); -  +Collections.sort (list,NewPricesort ());//notice this is collections . -         //after sorting by price +SYSTEM.OUT.PRINTLN ("Price Order: **********************"); A System.out.println (list); at         //Collection Positive Order -System.out.println ("Sort of collection: **********************"); -Collections.sort (list,NewFavorsort ()); - System.out.println (list); -         //name Positive Order -System.out.println ("name Sort: **********************"); inCollections.sort (list,NewNamesort ()); - System.out.println (list); to     } +}

Java Custom Sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.