0

我有一个 Class 数组Customer。我想Customer根据名为name.

这是我正在使用的代码:

int j;
        boolean flag = true;  // will determine when the sort is finished
        Customer temp;
        while ( flag )
        {
            flag = false;
            for ( j = 0;  j < count_customers;  j++ )
            {

                if ( customers[ j ].getName().toString().compareToIgnoreCase( customers[ j + 1 ].getName().toString() ) > 0 )
                {
                    temp = customers[ j ];
                    customers[ j ] = customers[ j+1 ];     // swapping
                    customers[ j+1 ] = temp; 
                    flag = true;
                 } 
             } 
         } 

customers[]Customer count_customers是代表活跃客户数量的数组。出于某种原因,当我运行下面的代码时,什么也不返回:

        for(int i=0; i < count_customers; i++)
        {
            tmp_results += customers[ i ].toString() + "\n";
        }

.toString()Customer类中定义,它只是打印出关于客户的所有信息。

那么我做错了什么?

4

1 回答 1

5

创建一个新类CustomerComparator并排序您的Customer[]使用Arrays.sort(array, new CustomerComparator());

public class CustomerComparator implements Comparator<Customer> {

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

}
于 2013-03-25T22:38:40.110 回答