对数组对象列表进行排序

时间:2018-02-13 20:44:54

标签: java csv sorting object arraylist

我正在尝试将包含4列的CSV文件中的数据读入数组列表。让我们调用列 a,b,c,d (它们各自包含整数)。然后我想根据a,b,c,d。

行的内容对数组列表进行排序

因此,如果您要比较第1行和第2行,如果 1d< 2d 的值则返回某个值。如果1d = 2d则将1c与2c进行比较,依此类推。我在找到一种方法来创建一个允许我区分和比较每一行/列的数组列表时遇到了麻烦。

public class Speed {
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        readCSV();
    }

    public static void readCSV() throws FileNotFoundException, IOException {
        BufferedReader br = new BufferedReader(new FileReader("amis.csv"));
        String line = "";
        ArrayList<String> amis = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            line = line.replaceAll("\"", "");

            amis.add(line);
        }

        amis.remove(0);

        for (String alphabet: amis) {
            Object[] parts = alphabet.split(",");
            Object studentID = (parts[0]);
            Object a = parts[1];
            Object b = parts[2];
            Object c = (parts[3]);
            Object d = parts[4];
            ArrayList<Object> Compare = new ArrayList();

            Compare.add(a);
            Compare.sort(new customComparator());
        }

我的自定义比较器类

public class customComparator implements Comparator<Object> {
    public int compare(Object o1, Object o2) {
        int a = (Integer) o1;
        int b = (Integer) o2;

        if (a < b) {
            return 1;
        }
        else if(a > b)
            return -1;
        else
            return 0;
    }
}

2 个答案:

答案 0 :(得分:0)

您应该为每个字符串和比较器创建POJO。在比较器中,您应首先比较更多“重要”列,如果前者相等则更不重要。

public class Pojo {
private int a;
private int b;
private int c;
private int d;

public int getA() {
    return a;
}

public void setA(int a) {
    this.a = a;
}

public int getB() {
    return b;
}

public void setB(int b) {
    this.b = b;
}

public int getC() {
    return c;
}

public void setC(int c) {
    this.c = c;
}

public int getD() {
    return d;
}

public void setD(int d) {
    this.d = d;
}

public static class PojoComparator implements Comparator<Pojo> {

    @Override
    public int compare(Pojo pojo1, Pojo pojo2) {
        return pojo1==null ? (pojo2==null ? 0:1) :(pojo2==null?-1:
                (pojo1.d!=pojo2.d? pojo1.d-pojo2.d : 
                (pojo1.c!=pojo2.c ? pojo1.c-pojo2.c: 
                (pojo1.b!=pojo2.b? pojo1.b-pojo2.b:
                        pojo1.a-pojo2.a))) );
    }
  }
  }

答案 1 :(得分:0)

让我们来看看您当前的排序方法

for(String alphabet: amis)
{
    Object[] parts = alphabet.split(",");
    Object studentID = (parts[0]);
    Object a = parts[1];
    Object b = parts[2];
    Object c = (parts[3]);
    Object d = parts[4];
    ArrayList<Object> Compare = new ArrayList();

    Compare.add(a);
    Compare.sort(new customComparator());
}

最明显的问题是你要创建一个ArrayList,比较,添加/排序,然后丢弃它

如果您正在尝试制作第二个排序列表:

List<String> sorted = new ArrayList<String>(amis);
sorted.sort(new CustomComparator());

如果您只是尝试对原始列表进行排序:

amis.sort(new CustomComparator());

通过制作比较器,你已经非常接近你想做的事了,但它需要调整

当前的实现在检查第一个值后停止,它返回0而不是继续

public class CustomComparator implements Comparator<String>
{
    public int compare(String A, String B)
    {
        String[] as = A.split(",");
        String[] bs = B.split(",");

        int a = Integer.parseInt(a[4]); //column d, as an int
        int b = Integer.parseInt(b[4]);
        if(a < b)
            return 1;
        else
            if(a > b)
                return -1;
            else
            {
                a = Integer.parseInt(a[3]); //column c, as an int
                b = Integer.parseInt(b[3]);
                if(a < b)
                    return -1;
                else
                    if(a > b)
                        return 1;
                    else
                    {
                        a = Integer.parseInt(a[2]); //column b, as an int
                        b = Integer.parseInt(b[2]);
                        if(a < b)
                            return -1;
                        else
                            if(a > b)
                                return 1;
                            else
                            {
                                a = Integer.parseInt(a[1]); //column a, as an int
                                b = Integer.parseInt(b[1]);
                                if(a < b)
                                    return -1;
                                else
                                    if(a > b)
                                        return 1;
                                    else
                                        return 0; //all columns are the same
                            }
                    }
            }
    }
}

注意到有很多类似的代码,我们可以将其改为循环

public int compare(String A, String B)
{
    String[] as = A.split(",");
    String[] bs = B.split(",");

    for(int i = 4; i >= 1; i--) //columns d-a
    {
        int a = Integer.parseInt(a[i]);
        int b = Integer.parseInt(b[i]);
        if(a < b)
            return -1;
        if(a > b)
            return 1;
    }
    return 0;
}