[英]Sorting through an array list of objects
我正在尝试从包含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;
}
}
您应该为每个字符串创建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))) );
}
}
}
让我们来看看您当前的排序方法
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.