[英]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.