cc

mail@pastecode.io avatar
unknown
java
2 years ago
3.2 kB
1
Indexable
Never
package demog4;

/**
 *
 * @author VO PHU
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Flower {

    int ID;
    String Name;
    double Price;
    String Color;

    Flower(int ID, String Name, double Price, String Color) {
        this.ID = ID;
        this.Name = Name;
        this.Price = Price;
        this.Color = Color;
    }


    public static void main(String[] args) {

        ArrayList<Flower> f = new ArrayList<Flower>();
        
        f.add(new Flower(1, "Hoa hong", 2.2, "hong"));
        f.add(new Flower(2, "Hoa sen", 3.2, "hong"));
        f.add(new Flower(6, "Hoa hong do", 4.2, "do"));
        f.add(new Flower(3, "Hoa lai", 2.2, "trang"));
        f.add(new Flower(5, "Hoa mai", 2.3, "vanh"));
        f.add(new Flower(4, "Hoa lan", 1.2, "trang"));
        f.add(new Flower(8, "Hoa hue", 2.5, "tim")); 
        f.add(new Flower(10, "Hoa dao", 6.2, "hong"));
        f.add(new Flower(9, "Hoa huong duong", 2.8, "vang"));
        f.add(new Flower(7, "Hoa cuc", 3.2, "trang"));
        System.out.println("before sorting: ");
        for (Flower flo : f) {
            System.out.println(flo.ID + " " + flo.Name
                    + " " + flo.Price + " " + flo.Color);
        }
       
        System.out.println();
        System.out.println("After sorting by id: ");
        Collections.sort(f,new Comparator<Flower>() {
            @Override
            public int compare(Flower o1, Flower o2) {
                return o1.ID - o2.ID;
            }
        });
        for (Flower flo : f) {
            System.out.println(flo.ID + " " + flo.Name
                    + " " + flo.Price + " " + flo.Color);
        }
        
        System.out.println();
        System.out.println("After sorting by name: ");
        Collections.sort(f,new Comparator<Flower>() {
            @Override
            public int compare(Flower o1, Flower o2) {
                return o1.Name.compareTo(o2.Name);
            }
        });
        for (Flower flo : f) {
            System.out.println(flo.ID + " " + flo.Name
                    + " " + flo.Price + " " + flo.Color);
        }
        
         System.out.println();
        System.out.println("After sorting by price: ");
        Collections.sort(f,new Comparator<Flower>() {
            @Override
            public int compare(Flower o1, Flower o2) {
                return (int) (o1.Price - o2.Price);
            }
        });
        for (Flower flo : f) {
            System.out.println(flo.ID + " " + flo.Name
                    + " " + flo.Price + " " + flo.Color);
        }
        
        System.out.println();
        System.out.println("After sorting by color: ");
        Collections.sort(f,new Comparator<Flower>() {
            @Override
            public int compare(Flower o1, Flower o2) {
                return o1.Color.compareTo(o2.Color);
            }
        });
        for (Flower flo : f) {
            System.out.println(flo.ID + " " + flo.Name
                    + " " + flo.Price + " " + flo.Color);
        }
    }
}