从文本文件中按名称列出客户

时间:2015-12-08 00:55:08

标签: java

我正在开展一个项目,我的客户对文本文件进行了预订。我希望能够" listCustomer"然后按客户名称按字母顺序排列。我现在所拥有的只有:

public static void listCustomers(List<Customer> customers) {  
    System.out.format("\033[31m%s\033[0m%n", "Customers");
    System.out.format("\033[31m%s\033[0m%n", "=========");
    for (Customer c : customers) {
        System.out.format("\033[33m%s\033[0m%n", "Customer"); 
        System.out.format("\033[33m%s\033[0m%n", "--------"); 
        System.out.println(c);
    }       
}   

这样可以列出客户,但它并不按字母顺序列出,因为我也喜欢它们。我有点新编码,所以如果这是一个愚蠢的方式来做它让我知道。谢谢:))

3 个答案:

答案 0 :(得分:2)

您可以使用import java.util.Comparator; public class CustomerComparator implements Comparator<Customer> { @Override public int compare(Customer c1, Customer c2) { return c1.toString().compareTo(c2.toString()); } } 方法来实现此目的。

您只需要自己实施比较器。

Collections.sort

这将按字典顺序(或者换句话说,按字母顺序)按升序比较客户的toString()值。 如果需要按客户的名称排序,请将toString()替换为相应的name属性。 (通常是getName())toString()将使用customers.sort()排序,因此如果您的toString()方法返回name属性,则只需输入public static void listCustomers(List<Customer> customers) { CustomerComparator comparator = new CustomerComparator(); customers.sort(comparator); System.out.format("\033[31m%s\033[0m%n", "Customers"); System.out.format("\033[31m%s\033[0m%n", "========="); for (Customer c : customers) { System.out.format("\033[33m%s\033[0m%n", "Customer"); System.out.format("\033[33m%s\033[0m%n", "--------"); System.out.println(c); } }

打印结果:

customers.sort(new Comparator<Customer>() {
    public int compare(Customer c1, Customer c2) {
        return c1.toString().compareTo(c2.toString());
    }
});

如果您想免除额外的课程,可以内联声明界面,如下所示:

public class test {

    public static void main(String[] args) {
        int num = 12345;
        String str ,numStr = Integer.toString(num);

        for (int j=1 ; j<numStr.length() ;j++) {
            for (int i=0 ;i<numStr.length() ; i++){
                if (i+j <=numStr.length()) {
                    str = numStr.substring(i, i+j);
                    System.out.println(str+",");
                }

            }
            System.out.println("-------------");
        }
    }

}

答案 1 :(得分:0)

Collections.sort(customers);

//然后用你现在拥有的相同逻辑打印

答案 2 :(得分:0)

你可以用下面的比较器来做。

<强> Main.java

public class Main {

    public static void main(String[] args) {

        List<Customer> customers = new ArrayList<Customer>();

        customers.add(new Customer("001", "Namal"));
        customers.add(new Customer("002", "Lakmini"));
        customers.add(new Customer("003", "Ahas"));

        System.out.println("main().customers 1 : " + customers);

        Collections.sort(customers,new NameSorter());

        System.out.println("main().customers 2 : " + customers);

    }

}

<强> Customer.java

public class Customer {

    public Customer(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return this.id + "|" + this.name;
    }

    private String name = null;
    private String id = null;

}

<强> NameSorter.java

public class NameSorter implements Comparator<Customer>{

    @Override
    public int compare(Customer o1, Customer o2) {
        return o1.getName().compareTo(o2.getName());
    }

}